I'm training in powershell 3.0 and i'm trying to get the content of a file in bytes, sending it through a pipeline in order to join the result as by default there's one byte per line, and then send the result in a file.
Here's the command I'm using:
get-content 'My file' -Encoding byte | $_ -join ' ' | Out-File -path 'My result file'
So to summarize, does someone know how to use a -join
after a pipeline?
In PowerShell, string concatenation is primarily achieved by using the “+” operator. There are also other ways like enclosing the strings inside double quotes, using a join operator, or using the -f operator. $str1="My name is vignesh."
The join operator concatenates a set of strings into a single string. The strings are appended to the resulting string in the order that they appear in the command.
You can't do the -join
via the pipeline because the pipeline stage only sees one object at a time.
Instead, treat the collection returned by get-content
as a single object and join that.
(get-content -path 'my file' -Encoding Byte) -join ' ' | out-file -path 'My result file';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With