Trying to remove all unlicensed users from the Global address list using Powershell

Viewed 1473

I'm trying to remove all users that are unlicensed from the global address list.

So far, I've found that ...

$mboxes = Get-MsolUser -All -UnlicensedUsersOnly

... will get me all unlicensed users, and put them in a variable called $mboxes, however there doesn't appear to be an Identity here.

For when I try and run something like:

foreach ($mbox in $mboxes) { Set-Mailbox -HiddenFromAddressListsEnabled $true -Identity $mbox }

I get back the following error.

Cannot process argument transformation on parameter 'Identity'.

I've tried exporting that data to a CSV and creating an "Identity" header, however I get the same issue after re-importing the data back into PowerShell.

1 Answers

Your solution was very close. From the Set-Mail docs:

The -Identity parameter specifies the mailbox that you want to modify. You can use any value that uniquely identifies the mailbox.

Use $mbox.UserPrincipalName to pass the User Principal Name.

$mboxes = Get-MsolUser -All -UnlicensedUsersOnly
foreach ($mbox in $mboxes) { Set-Mailbox -HiddenFromAddressListsEnabled $true -Identity $mbox.UserPrincipalName }
Related