Bulk Hide Mailboxes in Active Directory

Hide all users in a specific Organisational Unit.

$OU = "OU=Path,OU=Here,DC=domain,DC=com"
$Users = Get-ADUser -Filter * -SearchBase $OU

foreach ($U in $Users) {
	Write-Host "Processing [$($U.SamAccountName)]"
	try {
		Set-ADUser -Identity $U.SamAccountName -Add @{'msExchHideFromAddressLists'=$true} -ErrorAction "Stop"
		Write-Host " -- Hidden [$($U.SamAccountName)] in the GAL"
	} catch {
		Write-Host " -- Failed to hide [$($U.SamAccountName)] in the GAL: [$($_.Exception.Message)]"
	}
}

Hide a single user

try {
	Set-ADUser -Identity <SamAccountName> -Add @{'msExchHideFromAddressLists'=$true}
	Write-Host " -- Hidden [$($U.SamAccountName)] in the GAL"
} catch {
	Write-Host " -- Failed to hide [$($U.SamAccountName)] in the GAL: [$($_.Exception.Message)]"
}

Last updated