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)

cdpwdlspushdpopdcprmrmdirmvcatechosetpskillclearman

new file

LinuxPowerShell
touch namenew-item name -ItemType "file"

new dir

LinuxPowerShell
mkdir name mkdir name
or
new-item name -ItemType directory

find program

LinuxPowerShell
which cmdGet-Command cmd

file content

LinuxPowerShell
cat fnameGet-Content fname
cat fname1 fname2 > newFileNameGet-Content fname1, fname2 > newFileName
head -n 50 fnameGet-Content fname | select -first 50
tail -n 50 fnameGet-Content fname | select -last 50

unzip

LinuxPowerShell
unzip fname.zipExpand-Archive fname.zip

list files

LinuxPowerShell
find . -name "*html"Get-ChildItem -Recurse -include *html -name
find . -type dGet-ChildItem -Directory -Recurse -name
find . -type fGet-ChildItem -File -Recurse
find . -size 0Get-ChildItem -recurse | where {$_.length -eq 0}
find . -name "*~" Get-ChildItem -Include *~ -Recurse

delete file

LinuxPowerShell
find . -name "*~" -deleteGet-ChildItem -Recurse -Include *~ | Remove-Item

search text (grep)

LinuxPowerShell
grep regex *htmlselect-string *html -pattern regex -CaseSensitive

compare file

LinuxPowerShell
cmp f1 f2diff (cat f1) (cat f2)
diff f1 f2diff (cat f1) (cat f2)
or
Compare-Object (Get-Content f1) (Get-Content f2)

diff, sort, get column etc

LinuxPowerShell
sortSort-Object
uniqsort -Unique
wcMeasure-Object

empty trash

Clear-RecycleBin

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

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.

PowerShell

xah powershell logo

More Advanced