Get active mailboxes from main OU but exclude one sub OU

Hi All,

I need to get a list of all active mailboxes from the main OU excluding one sub OU.

Lets say the main OU is “ou=Users & Computers,dc=domain,dc=org” and the sub OU is “ou=Users,ou=Remote - 92,ou=Users & Computers,dc=domain,dc=org”

I have the below powershell script that works fine for all users. How to exclude users from the sub OU?

[pre]

Get-Mailbox -OrganizationalUnit “ou=Users & Computers,dc=domain,dc=org” -ResultSize Unlimited |
Select-Object DisplayName,PrimarySmtpAddress,@{label=“TotalItemSize(MB)”;expression={(Get-MailboxStatistics $).TotalItemSize.Value.ToMB()}},@{label=“LastLogonTime”;expression={(Get-MailboxStatistics $).LastLogonTime}} |
Export-Csv “C:\Scripts\ActiveMailboxes$(get-date -Format dd.MM.yy).csv”

[/pre]

Hey Neville,

You could add the Where-Object cmdlet to your oneliner.

[pre]
Get-Mailbox -OrganizationalUnit “ou=Users & Computers,dc=domain,dc=org” -ResultSize Unlimited |
where { $.DistinguishedName -notlike “OU=Remote - 92” } |
Select-Object DisplayName,PrimarySmtpAddress,@{label=“TotalItemSize(MB)”;expression={(Get-MailboxStatistics $
).TotalItemSize.Value.ToMB()}},@{label=“LastLogonTime”;expression={(Get-MailboxStatistics $_).LastLogonTime}} |
Export-Csv “C:\Scripts\ActiveMailboxes$(get-date -Format dd.MM.yy).csv”
[/pre]

Since filtering as much left as possible is best practice in PowerShell, I was trying to filter it by using the Filter parameter of Get-Mailbox. After some research, I found out that you can’t use the DistinguishedName property in this filter. Here for, you need to use the Where-Object Cmdlet.

EDIT: You’re able to use the DistinguishedName property in the Filter parameter, but only with the -eq comparison operator.

Regards,
Tom

Thanks Tom. That worked for me.