I need to add from a list of users to an AD security group.
I now work in a new company and no one know PowerShell (I'm so alone)
I've google and have come up with the script below.
My CSV will have a header called "SamAccountName" and the user ID's below it.
Here are my questions.
1. If the person is already a member, will the script continue to work? If not how do I make it continue?
2. How can I capture errors and send them to a file
3. How can I capture successful adds to the group and send them to a separate file (preferred if not it can be the same as with the errors)
4. How can I capture statements indicating the user is already a member of the group
5. The script currently will show users added, however, will is show errors on the screen?
6. Any suggestions to improve the script?
*******Code*******
Import-Module ActiveDirectory
$adGroup = "MyADGroup"
Import-Csv "C:\Scripts\Users.csv" | ForEach-Object {
$samAccountName = $_."samAccountName"
Add-ADGroupMember $adGroup $samAccountName;
Write-Host "- "$samAccountName" added to "$adGroup
}
Thank you