How to use powershell to return all exchange distribution groups for a user
10,790
That's what you need:
$Mailbox=get-Mailbox [email protected]
$DN=$mailbox.DistinguishedName
$Filter = "Members -like ""$DN"""
Get-DistributionGroup -ResultSize Unlimited -Filter $Filter
Get-DistributionGroup
are not recognize/expand the filter when it's in a {ScriptBlock}
You should create a $Filter
Variable that is not in a {ScriptBlock}
but it's inside a "Quotes"
from the outside and ""DoubleQuotes""
inside for the variable to expand.
Author by
PenNerd
Updated on June 15, 2022Comments
-
PenNerd almost 2 years
I'm trying to pull a list of Office365 distribution groups for a specific user using powershell. The following works when I type out (or paste)the captured $DN for the user, but if I capture the $DN and use it as a variable, I cannot get the correct results.
This works:
$mailbox=get-Mailbox [email protected] $DN=$mailbox.DistinguishedName $DLs=Get-DistributionGroup -ResultSize Unlimited -Filter {Members -like "CN=Lastname\, First M,OU=domain.onmicrosoft.com,OU=Microsoft Exchange Hosted Organizations,DC=NAMPR02A003,DC=prod,DC=outlook,DC=com"}
These do not:
$DLs=Get-DistributionGroup -ResultSize Unlimited -Filter {Members -like $DN} $DLs=Get-DistributionGroup -ResultSize Unlimited -Filter {Members -like '$DN'} $DLs=Get-DistributionGroup -ResultSize Unlimited -Filter {Members -like "$DN"}
Can anybody tell me how to get a variable for $DN to work in the script?
-
PenNerd almost 9 yearsThat did it. Thank you Avshalom. I massaged that script for hours but never thought to head that direction.
-
Avshalom almost 9 years@PenNerd You Welcome :)