question

BijuThankappan-2567 avatar image
0 Votes"
BijuThankappan-2567 asked BijuThankappan-2567 commented

Powershell to change email suffix of AD Contacts whose suffix is abc.com to xyz.com in a particular OU

I have the below code:
$LocalUsers = Get-ADObject -Filter {(objectClass -eq "contact") -and (mail -like '*gmail.com')} -SearchBase "OU=T,OU=Test,DC=contoso,DC=com" -Properties mail -ResultSetSize $null
$LocalUsers | foreach {$newUpn = $.email.Replace("gmail.com","contoso.com"); $ | Set-ADObject -mail $newUpn}

And get the below error:
Method invocation failed because [Microsoft.ActiveDirectory.Management.ADPropertyValueCollection] does not contain a method named 'Replace'.
At line:2 char:24
+ ... | foreach {$newUpn = $_.email.Replace("gmail.com","contoso.com"); ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound


Set-ADObject : A parameter cannot be found that matches parameter name 'mail'.
At line:2 char:99
+ ... Replace("gmail.com","contoso.com"); $_ | Set-ADObject -mail $newUpn ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADObject], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADObject


Thank you for your help!

Regards,
BT

windows-server-powershellwindows-active-directory
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered BijuThankappan-2567 commented

Hi,

For the first error, the property is "mail", not "email".
For the second error, the Set-ADObject cmdlet has no "-mail" parameter. Please use "-replace" to set the mail attribute.

 $LocalUsers = Get-ADObject -Filter {(objectClass -eq "contact") -and (mail -like '*gmail.com')} -SearchBase "OU=T,OU=Test,DC=contoso,DC=com" -Properties mail -ResultSetSize $null
 $LocalUsers | foreach {$newUpn = $_.mail.Replace("gmail.com","contoso.com"); $_ | Set-ADObject -Replace @{mail=$newUpn} }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks, that did the trick!

How do we know what are the other properties available like 'mail'? Could be any object.

0 Votes 0 ·

To return all the properties you could use -properties *.

0 Votes 0 ·

Thanks! Last query: How do we export to csv in this case?

0 Votes 0 ·
Show more comments