I have read here that when you run external commands in powershell, their output is always interpreted as a string or string array: https://stackoverflow.com/a/35980675/983442
I'm trying to process binary output from an external command, but it seems like PowerShell can only give me strings.
This leaves me wondering, what encoding is used to convert the binary data into strings? And also, how does it interpret newlines in order to divide the binary data into a string array? It seems to be splitting on the \n
character alone, but I'm sure it would also split on \r\n
.
Is there even a reliable way to take the strings powershell gives me and turn them back into a byte array?
For example, let's say I have a batch file with the following contents, call it thing.bat
:
@echo off
type image.jpg
I then run the following powershell:
PS> $x = & .\thing.bat
PS> $x.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS> $x[0].gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS> $x.count
36
How can I reliably recreate this image.jpg in PowerShell once I have the $x
variable?