I have 940 user accounts in one OU in Active Directory Windows 2012 R2 that needs hiding from the address list. I need a script, but I am struggling. Any ideas?

Identify accounts:

get-aduser -filter {-not(msExchHideFromAddressLists -eq $true)} -searchbase “OU=xxxxxx,dc=internal,dc=xxxxx,dc=org,dc=uk” -searchscope onelevel

Apply change

| set-aduser -replace @{msExchHideFromAddressLists=“TRUE”}

Resolved with some help from a friend.

Get-DistributionGroup -OrganizationalUnit "Contoso.contoso.com/....../........./DL Location" | Set-DistributionGroup -HiddenFromAddressListsEnabled $TRUE

this should do the job

ohh just saw it was for users, looks like your sorted now anyway

Thank you Mark, we must have posted simultaneously : )

| set-aduser -replace @{msExchHideFromAddressLists="TRUE"}

This does not actually hide the user in the GAL. The flag will be set, the box will be checked in Exchange, but the user is still visible. Hiding the entry also requires modification to showInAddressBook. If you don’t want to use the Exchange commands, you could just clear it, but the best way is to use the appropriate Exchange command to hide it, ex: Set-Mailbox, Set-MailUser.