Powershell to create Exchange global address list

I’m trying to create an Exchange global address list using a variable within the recipient filter part. I’m struggling with the exact format it requires. Below is what I’ve got, but you’ll see the result is that the filter is not using the value from the attribute but, instead, the attribute name. It is applying correctly when creating the name of the GAL.

$sitecode = "ABC"

 New-GlobalAddressList -Name "Global Address List - $sitecode" -RecipientFilter {(CustomAttribute7 -like "$sitecode*")}

Name                                   RecipientFilter
----                                        ---------------
Global Address List - ABC  CustomAttribute7 -like '$sitecode*'

I’m trying to get the RecipientFilter for the address book to be:

CustomAttribute7 -like “ABC*”

I’m sure I’m missing something obvious but can’t work out what. Any help would be appreciated. Thank you.

Did you carefully read the help for the parameter -RecipientFilter?

The type of the parameter is [STRING] - not scriptblock! :wink:

Enclose the whole OPath filter in double quotation marks " ". If the filter contains system values (for example, $true , $false , or $null ), use single quotation marks ’ ’ instead.

I cannot validate but I think it should something like this

-RecipientFilter "CustomAttribute7 -like '$sitecode*'"

or the other way around

-RecipientFilter 'CustomAttribute7 -like "$sitecode*"'

Thank you very much! The first one you suggested did the trick.

If I may ask a follow up question, I’m trying to create another line of script to create an AddressList but am getting stuck when having to use a system value and a variable. I’ve had a read through the link you provided which I believe says I need to enclose the filter with single quotation marks instead of double. I’m sure it’s probably just a case of a quotation or bracket in the wrong place, but I can’t work it out.

I’ve listed some of my attempts, along with the errors, below. (The actual line of code I have contains more filter options, but I’ve simplified it below as I’m sure once the below is working I’ll be able to expand it accordingly.)

This first attempt seems to ignore $null and just shows it as an empty space in the error:

New-AddressList -Name "All Rooms - $sitecode" -RecipientFilter "'Alias -ne '$null' -and (CustomAttribute7 -like '$sitecode*')"

Cannot bind parameter 'RecipientFilter' to the target. Exception setting "RecipientFilter": "Invalid filter syntax. For a description of the filter parameter syntax see the command help.
"'Alias -ne '' -and (CustomAttribute7 -like 'ABC*')" at position 1."

I then tried to remove the double quotation marks altogether, but again no luck:

 New-AddressList -Name "All Rooms - $sitecode" -RecipientFilter 'Alias -ne $null -and (CustomAttribute7 -like '$sitecode*')'

A positional parameter cannot be found that accepts argument 'ABC*)'.

I’ve also tried using double quotes around the variable where I’m now using single quote marks around the whole filter. Although it doesn’t error, it then (again) uses the variable name rather than variable value in the result:


New-AddressList -Name "All Rooms - $sitecode" -RecipientFilter 'Alias -ne $null -and (CustomAttribute7 -like "$sitecode*")'

Name                      DisplayName               RecipientFilter
----                      -----------               ---------------
All Rooms - ABC           All Rooms - ABC           ((Alias -ne $null) -and (CustomAttribute7 -like '$sitecode*'))

I think I’m close, but any help getting this working properly would be greatly appreciated.

Just to give the complete picture of where I’m trying to get to, the below is the full PowerShell line I’m using when not using a variable and just entering the value in statically:


New-AddressList -Name "All Rooms - ABC" -RecipientFilter {(Alias -ne $null) -and (CustomAttribute5 -like 'ABC*') -and (RecipientDisplayType -eq 'ConferenceRoomMailbox') -or (RecipientDisplayType -eq 'SyncedConferenceRoomMailbox')}

Name                      DisplayName               RecipientFilter
----                      -----------               ---------------
All Rooms - ABC           All Rooms - ABC           ((((((Alias -ne $null) -and (CustomAttribute5 -like 'ABC*'))) -and (RecipientDisplayType -eq 'ConferenceRoomMailbox'))) -or (RecipientDisplayType -eq
                                                    'SyncedConferenceRoomMailbox'))

Hmmmm … tricky.

I’m not sure but as I understood it it should be something like this

New-AddressList -Name "All Rooms - $sitecode" -RecipientFilter '$null -ne "Alias" -and CustomAttribute7 -like "$Sitecode*"'

But then the variable $Sitecode should be availabe at runtime. What’s probably not the case, right?

You could try to build the filter string needed for the paramter -RecipientFilter outside of the actual command line and provide as variable like this:

$RecipientFilter = '$null -ne "Alias" -and CustomAttribute7 -like "{0}*"' -f $sitecode
New-AddressList -Name "All Rooms - $sitecode" -RecipientFilter $RecipientFilter

Regardless of that - why do you actually need to use variables in this case? For my understanding you would not change address lists that frequently that it needed to be automated that much. How many address lists do you want to create?

Ah … and BTW: If you compare with $Null you should always place the $Null first. Search for it and you will find the explanation for that. :wink:

Again, thank you so much for your help! I’ve got the full filter working now with help from your advice above about building the filter as a variable. I have to admit, I don’t quite understand why this works, but happy it does!

I notice you used the following code, but I’m not sure exactly how this works.

CustomAttribute7 -like "{0}*"' -f $sitecode

Do you have any info or links to a site that explains what the above does and why it’s needed (i.e. how this inserts $sitecode in place of the “{0}”) in this context?

I need to create about 20 address lists altogether. Truth be told, in the time it has taken me to get the scripts working with variables I could have probably done it manually! But I’m on a quest to improve my PowerShell scripts so it has been a good learning experience and (dare I say), fun too :slight_smile:

In case it’s of help to anyone else who comes across this, below is the full script I used to achieve the above:

$sitecode = "ABC"
$RecipientFilter = 'Alias -ne $null -and CustomAttribute7 -like "{0}*" -and RecipientDisplayType -eq "ConferenceRoomMailbox" -or RecipientDisplayType -eq "SyncedConferenceRoomMailbox" '  -f $sitecode
New-AddressList -Name "All Rooms - $sitecode" -RecipientFilter $RecipientFilter

Great that it helped and thanks for sharing your solution.

Did you try to search by yourself? :wink: It’s called the "-f " or "Format " operator. You can use it to insert one or more variables into a given string.

https://social.technet.microsoft.com/wiki/contents/articles/7855.powershell-using-the-f-format-operator.aspx

https://ss64.com/ps/syntax-f-operator.html