Posts

Showing posts with the label active-directory

Powershell script to add users to A/D group from .csv using email address only?

Powershell script to add users to A/D group from .csv using email address only? Import-CSV "C:usersBalbahagwdesktoptest1.csv" | Foreach-Object { $aduser = Get-ADUser -Filter { EmailAddress -eq $_.'EmailAddress' } if( $aduser ) { Write-Output "Adding user $($aduser.SamAccountName) to groupname" Add-ADGroupMember -Identity tech-103 -Members $aduser } else { Write-Warning "Could not find user in AD with email address $($_.EmailAddress)" } } Script is working now, however it can't find the user in AD with the email address. The error message tells you precisely what went wrong. – Bill_Stewart Jun 29 at 19:54 You want -Members not -User. At a powershell prompt type: Get-Help Add-ADGroupMember -Detailed – EBGreen ...

Skip part of script on error

Skip part of script on error I've been banging my head against the wall for awhile on this one. No amount of Googling has yielded me any successful results thus far. Wondering if someone can give me a hand? # Load the PowerShell module for Active Directory Import-Module ActiveDirectory # For each computer in AD that is a member of the target group Get-ADGroupMember -Identity "CN=RenameComputer,CN=Users,DC=int,DC=example,DC=com" | ForEach { # Define the new name as the old one, minus one letter at the end $NewComputerName = $_.Name -replace ".$" # Perform the rename operation Rename-Computer -ComputerName $_.Name -NewName $NewComputerName -Force -PassThru -Restart -WhatIf # Remove the computer from the target group # THIS SHOULD NOT OCCUR IF THE RENAME ABOVE FAILED!!! Remove-ADGroupMember -Identity "CN=RenameComputer,CN=Users,DC=int,DC=example,DC=com" -Members $NewComputerName -WhatIf } TL;DR: Thi...