Script to add users to an AD group - export file with confirmation of what accounts where added.

Posted on 2014-08-18
Last Modified: 2014-08-18

I want to use the script below to set a task to add users to a group every night . The Users.txt file will be updated
by the application owner .

can someone help me modify it so the script outputs a file of the SamAccountNames that where added ?
something like  export-csv C:\PS\AddedtoXXXgroup_$((Get-Date -Format MM-dd-yyyy)).csv -NoTypeInformation

Foreach ($User in GC \\Server\Share\Users.txt){
GC C:\PS\groups.txt | % {
Add-ADGroupmember -identity $_ -member $User
Question by:MilesLogan
    LVL 40

    Accepted Solution

    Try following code which will create a report with status.. check and let me know if it works for you..
    $(Foreach ($User in GC \\Server\Share\Users.txt){
    GC C:\PS\groups.txt | % {
       $Group = $_
       #using try catch to capture the errors
            Add-ADGroupmember -identity $Group -member $User -ea stop
    	#create custom PS object to output the command status..
            New-Object PSObject -Property @{
    	GroupName = $Group
    	User = $User
    	Status = "Success"
    	New-Object PSObject -Property @{
    	GroupName = $Group
    	User = $User
    	Status = "Failed"
    }) | export-csv "C:\PS\AddedtoXXXgroup_$((Get-Date -Format MM-dd-yyyy)).csv" -NoTypeInformation

    Open in new window

    LVL 2

    Author Closing Comment

    Subsun .. many thanks as always ! wow
    LVL 40

    Expert Comment

    You are welcome!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In this previous article (, we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
    The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
    This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
    To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now