Question

I'm trying to bulk update a large amount of distribution lists using powershell, I've written this so far:

foreach ($Item in @(Import-Csv -Path "ranks.csv")) {
$Rank = $Item.Rank
Set-DynamicDistributionGroup -Identity "$Rank" -DisplayName "$Rank (NC)" -Name "$Rank (NC)" -RecipientFilter {(RecipientType -eq "UserMailbox") -and (Title -eq "$Rank") -and (Company -eq "Company")}
}

The Exchange command is correct and all works fine except for the (Title -eq "$Rank") part which doesn't parse the variable and just sends it like this:

(RecipientType -eq 'UserMailbox') -and (Title -eq '$Rank') -and (Company -eq 'Company')

I'm guessing it's something to do with the filter for RecipientFilter being inside curly braces but have no idea how to stop this from happening.

Any help would be much appreciated as I don't want to have to manually update a few hundred lists!

Was it helpful?

Solution

Does it work if you create the filter scriptblock using an expandable string?

foreach ($Item in @(Import-Csv -Path "ranks.csv")) {
$Rank = $Item.Rank
$filter = [scriptblock]::create("(RecipientType -eq 'UserMailbox') -and (Title -eq '$Rank') -and (Company -eq 'Company')")
Set-DynamicDistributionGroup -Identity "$Rank" -DisplayName "$Rank (NC)" -Name "$Rank (NC)" -RecipientFilter $filter
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top