Correct Encoding with PowerShell Remoting

by Jan 12, 2016

When you run a native console command via PowerShell remoting, special characters like German Umlauts will be damaged because remoting uses a rather limited encoding.

$command = { systeminfo.exe /FO CSV | ConvertFrom-Csv  }
Invoke-Command -ScriptBlock $command -ComputerName Server01

To correct this, you can run the native console command inside a background job on the remote machine. When you do this, the command is executed by a regular PowerShell, and encoding is corrected:

$command = { systeminfo.exe /FO CSV | ConvertFrom-Csv  }

$remotecode = 
{ 
    param($Code)
    $job = Start-Job ([ScriptBlock]::Create($Code)) -Name Job1
    $null = Wait-Job $job 
    Receive-Job -Name Job1
    Remove-Job -Name Job1
}  

Invoke-Command -ComputerName Server01 -ScriptBlock $remotecode -ArgumentList $command

Throughout this month, we'd like to point you to two awesome community-driven global PowerShell events taking place this year:

Europe: April 20-22: 3-day PowerShell Conference EU in Hannover, Germany, with more than 30+ speakers including Jeffrey Snover and Bruce Payette, and 60+ sessions (www.psconf.eu).

Asia: October 21-22: 2-day PowerShell Conference Asia in Singapore. Watch latest annoncements at www.psconf.asia

Both events have limited seats available so you may want to register early.

Twitter This Tip! ReTweet this Tip!