Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "-join" command after a pipeline

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?

like image 692
DoT Avatar asked Jun 26 '14 13:06

DoT


People also ask

How do I join a string in PowerShell?

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."

What is the purpose of a join operation in PowerShell?

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.


1 Answers

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';
like image 61
alroc Avatar answered Nov 23 '22 02:11

alroc