2

I'm currently trying to read the output of PowerShell from a pipe, by calling CreateProcess().

startInfo.cb = sizeof(STARTUPINFOW);
startInfo.hStdError = pipes->hErrWrite;
startInfo.hStdOutput = pipes->hOutWrite;
startInfo.hStdInput = pipes->hInRead;
startInfo.dwFlags = STARTF_USESTDHANDLES

BOOL success = CreateProcessW(
    L"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
    NULL,
    NULL,
    NULL,
    TRUE,
    0,
    NULL,
    NULL,
    &startInfo,
    &procInfo
);

Everything works fine, but the output of PowerShell has a different encoding.
I already did the same procedure with CMD. Luckily, CMD has the option to start it with the /U parameter, which encododed the output in UTF-16.

Is there something similiar for PowerShell? At the end of the day, I'd like to store the output of powershell in a wchar_t buffer.

1

1 Answer 1

3

PowerShell has no equivalent to cmd.exe's /U, unfortunately.

The only way to control the output encoding that I know of is to change [console]::OutputEncoding from within the PowerShell process, but I'm not positive that will work in all scenarios:

Here's the piece of PowerShell code you'd need to execute before producing any output (you'd have to pass that as part of the command to execute via the -Command parameter):

[console]::outputencoding=[text.encoding]::unicode

Here's an example from the cmd.exe command line:

powershell -command "[console]::outputencoding=[text.encoding]::unicode; 'Motörhead'" > out.txt

This would produce a UTF16-LE-encoded out.txt file containing the string Motörhead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.