0

I have a PowerShell script to show which mailboxes do not have the Exchange Retention Policy applied. The script works well, but I cannot figure out how to add an if condition that if all mailboxes do have the Retention Policy applied, then the following statement "Master Policy applied to all Mailboxes" is added to the text file.

Get-Mailbox -OrganizationalUnit "Users*" -ResultSize Unlimited -filter {RetentionPolicy -eq $null} |
  where {$_.RecipientTypeDetails -eq 'UserMailbox'} |
  select Alias > c:\retention.txt
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
morrowbrdz
  • 3
  • 1
  • 2

1 Answers1

0

Collect the results of Get-Mailbox in a variable:

$mailboxes = Get-Mailbox -OrganizationalUnit "Users*" -ResultSize Unlimited `
               -Filter {RetentionPolicy -eq $null} |
               ? {$_.RecipientTypeDetails -eq 'UserMailbox'} | select Alias

then check if the result is $null (no matches found) and create the output accordingly:

$outfile = 'C:\retention.txt'
if ($mailboxes -ne $null) {
  $mailboxes > $outfile
} else {
  'Master Policy applied to all Mailboxes' > $outfile
}

Replace > with >> if you want to append to the file.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thank you for the response. I understand the logic but I cant seem to get around the error "a positional parameter cannot be found that accepts argument $null." – morrowbrdz Jul 16 '14 at 18:30
  • Disregard. I got it to work. I had an extra ' in the script. Thank you for your help. – morrowbrdz Jul 16 '14 at 19:24