I am trying to get a list of all Exchange mailboxes that have inbox rules. I am able to get a list of all mailboxes and a count of how many rules each mailbox has. I am trying to skip or have no output for mailboxes with zero rules.
$mailboxes = get-mailbox
foreach ($mailbox in $mailboxes) {
Write-Output $mailbox.id,((Get-InboxRule -Mailbox $mailbox.id)| Measure-Object | select count)
}
Current code outputs: User1 0 User2 11 User3 0 User4 1 User5 0 etc....
I am looking to only output user's who have inbox rules.
Thanks-
CodePudding user response:
Not exactly sure what your expected is but to answer how to exclude those mailboxes with no inbox rules:
foreach($mailbox in Get-Mailbox) {
$ibxrules = @(Get-InboxRule $mailbox.Id).Count
if($ibxrules -eq 0) {
continue
}
[pscustomobject]@{
MailboxId = $mailbox.Id
InboxRules = $ibxrules
}
}
To explain the use of @(...)
around Get-InboxRule
, this is to ensure the output is always an array, and this also ensures we can always get a .Count
out of it.
CodePudding user response:
#!/usr/bin/env powershell
get-mailbox -resultsize unlimited |
ForEach-Object {
Write-Output -Message ('Checking {0}...' -f $_.alias) -Verbose
$inboxrule = get-inboxrule -Mailbox $_.alias
if ($inboxrule) {
foreach($rule in $inboxrule){
New-Object -TypeName PSObject -Property @{
Mailbox = $_.alias
## you could uncomment this if you wanted more information
##Rulename = $rule.name
##Rulepriority = $rule.priority
##Ruledescription = $rule.description
}
}
}
} |
Export-csv -Path "$env:userprofile/desktop/export.csv" -NoTypeInformation
This should give you a list of just the users who have rules.