I'm trying to extract select sets of users with Get-ADUser. The users belong to companies whose names include non-ASCII characters, e.g. "Gåäördet". Unfortunately, we do have to use the company property for this task and we also need it to work from a script.
The following works great in an interactive session but returns no data when executed within a script:
$Company = "Gåäördet"
Get-ADUser -Filter "company -eq '$Company'"
The workarounds I've found work but are not reliable enough (risks selecting wrong objects):
# Work-around 1:
$Company = "Gaaordet" # Replace åäö with aao in the variable
Get-ADUser -Filter "company -eq '$Company'" # Matches the company "Gåäördet", but why?
…or…
# Work-around 2:
$Company = "G...rdets" # Use regex for åäö
Get-ADUser -Filter * -Properties Company | ? Company -match "$Company"
For additional note: character encoding might not be the issue here. As suggested in a comment I put this within a script. Read the comment for each Get-ADUser-line:
$OutputEncoding = [Console]::OutputEncoding
$Company = "aao"
Get-ADUser -Filter "company -eq '$Company'" # Matches company "åäö"
$Company = "åäö"
Get-ADUser -Filter "company -eq '$Company'" # No matches
I was hoping some of you could offer a better solution to this conundrum.
Best Answer
Thanks to all the help here I got to the bottom of this odd behavior, much appreciated!
Turns out the "-Filter" argument accepts "åäö" interchangeably with "aao". This is not the doing of PowerShell but further down the stack (thanks @RyanRies for looking into it). That's the reason why the following snippet works:
It also turns out the query is not case sensitive, so this works too:
Actually, "åäö" works too as long as it is a unicode query (thanks @Daniel):
In the end this leaves us with two options:
I chose to go with the second option and the outcome looks a bit like this:
I guess that is as good as it gets for now.