PowerShell tips for bash users, part 1

PowerShellSome software testers often write shell scripts to make life a bit easier. But real power of shell lies in shell itself, in so called “oneliners” which UNIX and Linix admins (and also testers in UNIX environments which I used to be) so much love and use. When transfered to Windows environments, those guys will probably continue to use tools they are comfortable with. There are myriad of UNIX shell ports (for bash shell there is Win-bash) and emulators (Cygwin) out there to help create UNIX or Linux like environments in Windows. However, sometimes one is stuck with pure Windows…

Windows Servers are shipping lately with scripting environment called PowerShell (ex Monad AFAIR). If you use Core installation of Windows server, PowerShell is more or less the only thing you have. I will give here few quick oneliner PowerShell tips for people used to work with bash and UNIX command line in general. In this first article in a series we will be talking about: backticks, command separators, newlines, blanks, xargs, tr and cut.

Backticks, command separators and new lines

In bash, stdout output of every command is text, so commands can be executed inline and operated on its output right away:


$ echo "dir1 dir2" > dirs
$ for a in `cat dirs`; do mkdir $a; done


This will create directories dir1 and dir2 in working directory. This also illustrate how to write for loop in one line. However, even if “do” and “done” are shell keywords, not commands, semicolon (or new line) is needed for separation as if we have list of commands.

PowerShell equivalent:


PS> echo "dir1" "dir2" > dirs
PS> foreach ($a in cat dirs) {mkdir $a}


Second PowerShell line is more readable because of the use of brackets and braces and there is also no need for backticks apparently. However, note that we have to separate strings to be echoed to file to force new line. If strings are written in the same line as with ‘echo “dir1 dir2” > dirs’, mkdir (standard cmd.exe) will create one directory named “dir1 dir2.”


Xargs is one of the most powerfull UNIX commands. It is used to build and execute command lines from standard input. For example:
$ cat dirs | xargs mkdir
will use cat to take the strings (be it newline or blank character separated) from file ‘dirs’ and pass them through pipe to xargs which will then send one by one line as argument to mkdir which will then create those dirs or complain if those are existent.

PowerShell equivalent:


PS> cat dirs | %{mkdir $_}


There is no ‘xargs’ command in PS, but you can use ‘foreach ‘ loop and pass the piped variable ‘$_’ to the mkdir. Shorthand for ‘foreach’ is ‘%’. This time also only newlines will separate the strings apart. If multiple strings separated by blanks are found in same line, mkdir will create a directory with blanks in the name, while we must quote to have the same in bash:


$ cat dirs | sed 's|^|"|g' | sed 's|$|"|g' |xargs mkdir



Cut is another useful UNIX command. It us used to cut sections from each line of output or file. For example:


$ echo 'why:me ' | cut -d ':' -f1


will print “why” because cut will cut everything behind delimeter “:”

Powershell has no simmilar cmdlet but has a neat function ‘split’ which can do the same:


PS> echo 'why:me' | %{$_.split(':')[0]}


but, mind the syntax. Even if it is completely understandable, it is not handy.


Ever once in a while one must to replace some character of text output with another. Let say slash with backslash:



$ echo 'why/not' | tr '/' '\\'


Powershell again has no tr command, but do has a “-replace” parameter which expects input from another PS cmdlet. Piped output is also ok:


PS> echo 'why/not' | %{$_ -replace '/', '\'}


Again mind the synthax. “-replace” used here is not the function which operates on variable, therefore there is no dot (do not confuse this with Replace function in PS). Also, seems we do not have to escape neither of slashes. However, while tr works only with characters, replace can be used on whole strings while on UNIX we would use sed or awk for string replacement.