Help with creating multiple variables and adding info to them

Hi,

I’m trying to create one variable to each distribution list I got in Exchange Online, then, add to that variables the members of each group. So I wrote this code:

$Lists = Get-DistributionGroup | Sort Name

foreach ($List in $Lists){
New-Variable $List
${$List} = Get-DistributionGroupMember -Identity $List
}

The problem is that, when I try to see the content of the variables, they are empty!
I need those variables filled with the members so I can use the results to check with every user on another foreach code…
Any1?

Fixed it by using this:

New-Variable $List -Value (Get-DistributionGroupMember -Identity $List.DisplayName)

Ok… Even so I can’t resolve what I need…

Write-Host "Getting all Exchange Online users ..." $Users = Get-Mailbox -RecipientTypeDetails UserMailBox | Sort Name Write-Host "Getting all distribution groups..." $Lists = Get-DistributionGroup | Sort Name

Write-Host "Getting all members of " $Lists.Count “distribution groups…”
foreach ($List in $Lists){
New-Variable $List -Value (Get-DistributionGroupMember -Identity $List.DisplayName)
}

Write-Host “Check the user memberships”
foreach ($User in $Users) {
Write-Host “The user” $Usuario “is member of this groups:”
foreach ($List in $Lists){
if (${$List}.Name -contains $User.Name){
Write-Host $List.DisplayName
}
}
}

The problem is that the “if” statement isn’t working as I want it to… And it’s printing all the distribution groups.
How can I make so the ${$List} changes into ${List - Support} for example?

Solved using it like this:

foreach ($List in $Lists){ if ((Get-Variable -Name $List -ValueOnly).Name -contains $User.Name){ Write-Host $List.DisplayName } }

Thanks anyway!