Forum Discussion
PSNewie2021
Aug 26, 2021Copper Contributor
Update AD Users' Email Address from CSV where this field is empty in AD (except specific domain)
Hi There, I am trying to create a PS script with the following conditions and hoping someone here is able to guide me. Any assistance would be highly appreciated. Import users from a CSV file...
psophos
Sep 20, 2021Brass Contributor
PSNewie2021summat like:
foreach($user in $Users)
{
$adUser = Get-ADUser -Identity $user.AccountName -Properties EmailAddress -ErrorAction SilentlyContinue
if($adUser)
{
# user exists
if([System.string]::IsNullOrWhiteSpace($adUser.EmailAddress) )
{
# email address is empty, set it
$adUser | Set-ADUser -EmailAddress $User.Email
}
elseif($adUser.EmailAddress -like '*mycompany.com')
{
# Leave this email alone.
}
else
{
# email address exists, what to do here?
}
}
else
{
# The user does not exist. This is probably an error of soem sort.
}
}Assuming the Get-ADUser line succeeds of course.
- kevin_devonApr 11, 2022Copper Contributor
Good Daypsophos I like the idea of this, but I want to use it for something other then email.
Is this possible? IE, I need to modify the homePostalAddress, this is currently either clear or has a License code using A2B1 etc etc. I need the blanks to have this A2B1C1D1, the reason I cannot run a script to ovewright everything is everyone is not the same, some have A2B1C2D1J1 and some have A2B1C1D1J4 etc,
- Newbie_JonesApr 11, 2022Brass Contributor
General etiquette would be to start your own thread for this question.
If you just want to set the field if its blank, then this is a two step process.
Not tested, but it will be something like.Get-ADUser -properties homePostalAddress -filter 'homePostalAddress -notLike "*"' | ForEach {Set-ADuser -Identity $_ -Add @{homePostalAddress = 'A2B1C1D1'}}