Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use cmd type pipe (/piping) in PowerShell?

In cmd (and bash), pipe "|" pushes output to another command in the original format of the first command's output (as string).

In PowerShell, everything that comes out the pipe is an object (even a string is a string object).

Because of that, some commands fail when run in a PowerShell command window as opposed to a Windows command window.

Example:

dir c:\windows | gzip > test.gz

When this command is run in the Windows command prompt window it works properly - directory listing of C:\windows gets compressed into test.gz file.

The same command in PowerShell fails, because PowerShell does not use cmd-style pipe and replaces it with PowerShell pipe (working with array of file system items).

Q. How do you disable the default piping behavior in PowerShell to make traditional Windows commands work identically in PowerShell?

I tried using the escape character "`" before the pipe "`|", but it didn't work. I also tried invoke-expression -command "command with | here", but it also failed.

like image 436
martind Avatar asked Dec 21 '22 16:12

martind


1 Answers

if you want to send strings down the pipeline you can use the cmdlet "out-string"

For Example:

get-process | out-string

If you are specifically looking for a PowerShell way to zip up files, check out the PowerShell Community Extensions. there are a bunch of cmdlets to zip and unzip all kinds of files.

http://pscx.codeplex.com

like image 84
Andy Schneider Avatar answered Jan 10 '23 07:01

Andy Schneider