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.
You want -Members not -User. At a powershell prompt type:
Get-Help Add-ADGroupMember -Detailed
– EBGreen
Jun 29 at 19:54
Get-Help Add-ADGroupMember -Detailed
Dude, don't invent parameters that don't exist. PowerShell tends to take issue with that. Read up on the cmdlet here: docs.microsoft.com/en-us/powershell/module/addsadministration/…;
– Rohin Sidharth
Jun 29 at 19:58
The object that I'm trying to import can't be found under the domain
– Belair Albahagwi
Jun 29 at 20:08
Instead of lambasting a user for misunderstanding an API, maybe try answering the question with how they can do it correctly and explain where they were wrong.
– Bender the Greatest
Jun 29 at 20:44
1 Answer
1
You need to first resolve the ADUser
object matching that email address, the -Identity
parameter won't auto-resolve based on the EmailAddress
field of an ADUser
. Assuming the EmailAddress
property is set appropriately on the user object in AD, and assuming the column name for the email address in your CSV is ExternalEmailAddress
, this should work:
ADUser
-Identity
EmailAddress
ADUser
EmailAddress
ExternalEmailAddress
Import-CSV "C:usersuserdesktoptest1.csv" | Foreach-Object {
$aduser = Get-ADUser -Filter "EmailAddress -eq '$($_.EmailAddress)'"
if( $aduser ) {
Write-Output "Adding user $($aduser.SamAccountName) to groupname"
Add-ADGroupMember -Identity groupname -Members $aduser
} else {
Write-Warning "Could not find user in AD with email address $($_.EmailAddress)"
}
}
Note that if the ADUser does not have the email address set, you will not be able to match that AD user to an email.
Here are the docs for Add-ADGroupMember
, you may want to read up on them for more information: https://docs.microsoft.com/en-us/powershell/module/activedirectory/add-adgroupmember?view=winserver2012-ps&viewFallbackFrom=winserver2012r2-ps
Add-ADGroupMember
EDIT: Found some strangeness with using brackets and the $PSitem, so I changed it to use a string-based filter.
EDIT 2: Found the cause for why using a variable in a bracket-based -Filter
doesn't work (which is how I had originally written this), and in fact is not recommended when scripting: Get-Aduser -Filter will not accept a variable
-Filter
I'm receiving a new error that the email address is not found in object of type
– Belair Albahagwi
Jun 29 at 21:14
Not sure which version of the AD cmdlets or Powershell you are running, you might try adding
-Properties EmailAddress
parameter to the Get-ADuser
cmdlet. But the code as I wrote it above is working for me without having to specify the additional EmailAddress parameter.– Bender the Greatest
Jun 29 at 21:23
-Properties EmailAddress
Get-ADuser
Nvm... Command worked! but the script can't find the email address. However, when I open the exchange console and when I run get-aduser the email address is there...
– Belair Albahagwi
Jun 29 at 21:24
What do you mean the script can't find the email address? Is it outputting "WARNING: Could not find user in AD with email address...."?
– Bender the Greatest
Jun 29 at 21:27
Yes. The script worked but the output is saying WARNING: Could not find user in AD with email address. I used the get-aduser -identity user -properties emailaddress command and the email is there
– Belair Albahagwi
Jun 29 at 21:34
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The error message tells you precisely what went wrong.
– Bill_Stewart
Jun 29 at 19:54