Powershell – Need PowerShell script to automatically hide disabled users from GAL in Exchange 2010

exchange-2010powershell

I'm looking for a PS script that will automatically hide all disabled users from the GAL. I ran the below command but it didn't actually hide anything so I'm thinking I must be missing something.

get-mailbox -ResultSize unlimited | where{$_.UserAccountControl -eq "AccountDisabled, NormalAccount" -and $_.RecipientTypeDetails -eq "UserMailbox"} | Set-Mailbox -HiddenFromAddressListsEnabled $True

Thanks in advance.

Best Answer

Try this out (haven't run it myself):

Import-Module C:\Temp\Exchange.psm1
$filter = "(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=2))"
$users = ([adsiSearcher]$Filter).findall()
foreach($suser in $users)
    {
    if($suser.properties.item("showInAddressBook") -ne $null) {
        get-mailbox "$($suser.properties.item("sAMAccountName"))" | ? {$_.RecipientType -eq "UserMailbox"} | set-mailbox -HiddenFromAddressListsEnabled $True
    }
}

Notes:

  • Run this in the Powershell ISE, not the Exchange Management Shell.
  • You will need to change the "Import-Module" line at the top to point to the Exchange.psm1 file on your Exchange management machine for this to work.
  • I would try just printing the user accounts that you are going to hide first to make sure it is going to affect the users you think it is before you configure their mailboxes. (Put a # before the pipe to set-mailbox to comment that part out)
Related Topic