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?
# 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.