Newb question - Spaces in output

Hello everyone,

I haven’t worked with PS in over a year and even then I wasn’t that experienced with it. I’m trying to see a simple script to remove all ProxyAddresses for a deprecated domain from my Active Directory accounts. However, the output is coming back with spaces after SMTP: and after the mailnickname field. Can someone tell me how to get rid of these spaces so the command works? Here is the script:

$ADobjects = @(Get-ADObject -Filter ‘objectClass -eq “User”’ -Properties mailNickname, ProxyAddresses) | Where-Object {$_.ProxyAddresses -Match “”}
ForEach ($userObject in $ADobjects)
ForEach ($proxyAddress in $userObject.ProxyAddresses)

Set-ADUser $userobject.mailnickname -Remove ‘@{ProxyAddresses="SMTP:’$userObject.mailnickname’}’


Why are you performing set foreach proxy address?

We don’t have to specify the .mailnickname property after set-aduser, the cmdlet is capable of binding the object to set-aduser. We wouldn’t use this property for identity anyway.

I suspect you have issues in your qoutes. Singles are literal and I don’t see the end double qoute. Use write-host to troubleshoot.

Get the value of what proxy you are looking for and remove it. No sense building a string of data that is already present.

Lastly improve your filter. get-aduser -Filter ‘anr -like “smtp:*”’

Always work with one object first until you know for sure what it’s doing.

$users = get-aduser -Filter 'anr -eq ""' -properties proxyaddresses

$users | %{

$proxytoremove = $_.proxyaddresses -match ""

Set-ADUser $_ -Remove @{ProxyAddresses=$proxytoremove} -whatif


And don’t ever, ever, ever use % as an alias for foreach-object in a production script

Is there a technical reason for that or just opinion?

This would fit into the one off typed into shell never saved category:D


It best practice to use the cmdlet instead of alias. I can use ? in a statement like | ? {$.something -like something} or I can do the same thing using the cmdlet like so: | where {$.something -like something}. It’s harder to read when using the alias instead of the cmdlet.


That’s fine but still no documentation that says never ever ever. Just like the write-host opinions.

I do understand what they are for:D