Skip to content

Instantly share code, notes, and snippets.

@CharlesB2
Created April 17, 2013 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlesB2/5404653 to your computer and use it in GitHub Desktop.
Save CharlesB2/5404653 to your computer and use it in GitHub Desktop.
#Using Start-Process you can get the correct encoding of your program output.
#However you have to use a temp. filename because it's the only thing Start-Process allows to.
#I wrote this function, taking program path and arguments, and returns stdout decoded from
#program output as UTF8 Function
Start-ProcessClean
{
Param( [Parameter(Mandatory=$True, Position=1)][string]$FilePath,
[Parameter(Mandatory=$False,Position=2)][string[]]$ArgumentList )
$stdOutTempFile = [IO.Path]::GetTempFileName()
$stdErrTempFile = [IO.Path]::GetTempFileName()
Start-Process $FilePath -ArgumentList $ArgumentList -NoNewWindow -Wait -RedirectStandardOutput $stdOutTempFile -RedirectStandardError $stdErrTempFile
$stdout = Get-Content $stdOutTempFile -Encoding UTF8
rm $stdOutTempFile; rm $stdErrTempFile
return $stdout
}
#Call it like
$out = Start-ProcessClean "program.exe" 'first_argument "argument with spaces"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment