PowerShell vs Bash

By Xah Lee. Date: . Last updated: .

This pages shows the equivalent of PowerShell for common unix commands related to text processing.

Simple Commands

The following bash commands have PowerShell alias. (but the options may not be the same)


file content

PurposeBash + GNU utilsPowerShell
create new filetouch ffni ff -type file
(ni = New-Item)
show file contentcat ffcat ff
(cat = Get-Content = gc)
merge filecat f1 f2 > new.txtcat f1, f2 > new.txt
show first n lineshead -n 50 ffcat ff | select -first 50
show last n linestailcat file | select -last 50
unzipunzip x.zipExpand-Archive -LiteralPath 'c:/Users/xah/Downloads/x.zip'

find file

PurposeBash + GNU utilsPowerShell
list dirsfind . -type dgci . -Recurse -name
(gci = Get-ChildItem)
list filesfind . -type f?
list html filesfind . -name "*html"gci . -Recurse -name -include *html
list empty filesfind . -size 0ls . -recurse | where {$_.length -eq 0}
delete empty filesfind . -type f -size 0 -exec rm {} \;?

search text (grep)

PurposeBash + GNU utilsPowerShell
search textgrep xyz f.txtselect-string f.txt -pattern xyz -CaseSensitive
search textgrep xyz *htmlselect-string *html -pattern xyz -CaseSensitive
search textgrep -iselect-string without -CaseSensitive
search textgrep --invert-matchselect-string with -NotMatch
search textgrep --files-with-matches

cat file_name | where { $_ -match regex_str}

cat file_name | %{$_ -replace regex_str, replace_string}

diff, sort, get column etc

PurposeBash + GNU utilsPowerShell
(diff = compare = Compare-Object)
compare file differencediff f1 f2diff (cat f1) (cat f2)
print nth columnawk '{print $12 , $7}'?
(sort = Sort-Object)
uniqsort -Unique
(measure = Measure-Object)

Work In Progress

Note: this page is a mess. It's work in progress. (started when i wished to convert my log processing bash script to PowerShell)

todo, show PowerShell equivalent of this:

find . -print0 | xargs -0 -l -i echo "{}";
find . -name "*bmp" -print0 | xargs -0 -l -i basename -s ".bmp" "{}" | xargs -0 -l -i convert "{}.bmp" "{}.png". 


# creating a new file in current dir
touch myfile.txt
# creating a new file in current dir
new-item -name myfile.txt -type "file"

Redirect “>”

# put content in a file
echo "some" > myfile.txt
echo "some more" >> myfile.txt # append
# put content in a file
"some" > myfile.txt
"some more" >> myfile.txt # append

Note that, by default, the PowerShell redirect operator ">" creates files with little endian utf-16 encoding, and lines are hard-wrapped at 80 chars, and line ending uses Windows convention of "\r\n" (ascii 13 and 10).

On unixes, the conventional file encoding is utf-8, and lines are not hard-wrapped (sometimes truncated (deleted) silently), and line ending uses "\n" (ascii 10).

To create unix style output, use out-file, like this:

"1'n2'n3" | out-file -Encoding utf8 -width 999000 myfile.txt

However, the line ending used is still "\r\n". To create unix line ending of just "\n", use:

… | Out-String | %{ $_.Replace("`r`n","`n") } | out-file

However, the end of the file will still contain a "\r".


Unix “cat” can be used to read a file, or join several files. PowerShell equivalent is “get-content” with alias “cat” too.

# display a file content. (cat is alias of get-content)
cat myfile.txt

Note that by default, PowerShell assumes ascii. You can set your $OutputEncoding like this:

# set $OutputEncoding to utf-8
$OutputEncoding = New-Object -typename System.Text.UTF8Encoding

Thanks to Shivashis Saha for addition on “cat”. He also sends the following:


For example, if you want to split a line based on “:”, you can use the following line:

(given $str is a line with different fields separated by ":")
$temp=@($str -split ":"); 

Super thanks to Jeffrey Snover of Microsoft for helping on about 10 of the items. (Jeffrey's the inventor of PowerShell)

If you have a question, put $5 at patreon and message me.


xah powershell logo