Newb question - Spaces in output

This topic contains 6 replies, has 4 voices, and was last updated by  Dan Potter 1 year, 7 months ago.

  • Author
  • #50610

    Tony DiLeone

    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'}'


  • #50621

    Dan Potter

    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:*"'

  • #50623

    Dan Potter

    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
  • #50653

    Richard Siddaway

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

  • #50657

    Dan Potter

    Is there a technical reason for that or just opinion?

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

  • #50663

    Wilfredo Perez


    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.


  • #50665

    Dan Potter

    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

    • This reply was modified 1 year, 7 months ago by  Dan Potter.

You must be logged in to reply to this topic.