In C and C++ command-line programs, is there any differences between running your programs within command prompt or within PowerShell? (e.g.: exception handling, I/O speed, etc.)
2 Answers
Update:
PowerShell (Core) v7.4+ now does support raw byte handling with external programs - see this answer.
The following therefore applies only to Windows PowerShell and PowerShell (Core) 7.3-
The main difference:
cmd.exeprovides true binary (byte-stream) conduits, so that>, the redirection operator, can capture an external program's raw byte output.Windows PowerShell and PowerShell (Core) up to v7.3 only ever use text (strings) to communicate with external programs, both on in- and output, which means that external-program output is invariably decoded to .NET strings, which has the following implications:
Even when only using PowerShell's
>operator to send an external program's output to a file, each line output by an external program is first decoded into a .NET string and then, on saving to the target file, encoded again, in this case using theOut-Filecmdlet's default encoding, which the>operator is an effective alias of.In Windows PowerShell, that (cmdlet-specific) encoding is UTF-16LE ("Unicode"), whereas in PowerShell (Core) 7, it is BOM-less UTF-8, the encoding used consistently in that PowerShell edition.
- To use a different encoding for saving, either use
Out-Fileexplicitly and pass it an-Encodingargument, or, for better performance with input that is already text - as is the case with external-program output -Set-Content.[1]
- To use a different encoding for saving, either use
This decoding-reencoding cycle not only slows things down, but also means:
For text output, the input character encoding (as decoded from the external program) may be different from the output-by-PowerShell character encoding.
Binary in- and output is fundamentally unsupported.
- The simplest workaround is to delegate to
cmd.exewithcmd /c ...(on Windows) and to/bin/shwithsh -c ...(on Unix-like platforms.
- The simplest workaround is to delegate to
See this answer for more information.
[1] Note that -Encoding Utf8 invariably creates UTF-8 files with BOM in Windows PowerShell - see this answer for workarounds in case BOM-less files are needed.
Comments
The program itself is just a process, it doesn't really matter how you launch it.
The notable difference is that, while both consoles connect themselves to the standard input and output streams of the process, everyone has its own performance while renderizing it, so the IO calls may be more or less impacted by this.
Technically, a console is just a program that invokes your program and graphically shows your output (and other things).