PowerShell vs Bash

By Xah Lee. Date: . Last updated: .

2021-06-21 this page is no longer maintained. Content has been expanded with more detail and categorized. See the navigation panel at index page: PowerShell Tutorial. For linux commands, see Linux: Basic Shell Commands


This pages shows the common tasks in both bash and PowerShell.

Simple Commands

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

cdpwdlspushdpopdcprmrmdirmvcatechosetpskillclearman

New File

touch name
new-item name -ItemType "file"

New Directory

mkdir name
mkdir name
# full syntax
new-item name -ItemType directory

Copy Directory

cp -r sourceDir destDir
cp -r sourceDir destDir

# full syntax:

# copy all dir's content, including all subdirs
Copy-Item -Recurse -Path "c:/Users/john/abc/*" -Destination "f:/my_backup/"

# copy dir and subdirs, including the dir in destination
Copy-Item -Recurse -Path "c:/Users/john/abc/" -Destination "f:/my_backup/"

Directory Size

# current dir size, in kilo bytes. includes subdirs
du . -sk
# size of current dir (counting subdir)
$size = gci -Recurse | Measure-Object -Property Length -sum

# in megabytes, round to 2 decimal places
"{0:N2}" -f ($size.sum / [Math]::pow(10,6))

find program

which myCommandName
gcm myCommandName
# full syntax
Get-Command myCommandName

Print File Content

cat fname
cat fname
# full syntax
Get-Content fname

Print First/Last n Lines of File

head -n 50 fname
Get-Content fname | select -first 50

Print last 50 lines of file

tail -n 50 fname
Get-Content fname | select -last 50

Join Files

cat fname1 fname2 > newFileName
Get-Content fname1, fname2 > newFileName

unzip

unzip fname.zip
Expand-Archive fname.zip

List Files by File Name Extension

find . -name "*html"
Get-ChildItem -Recurse -include *html

List Backup Files

find . -name "*~"
Get-ChildItem -Include *~ -Recurse

List Directories

find . -type d
Get-ChildItem -Directory -Recurse

List Files Not Directory

find . -type f
Get-ChildItem -File -Recurse

List Empty Files

find . -size 0
Get-ChildItem -recurse | where {$_.length -eq 0}

List Empty Directories

# list empty dir
Get-ChildItem -Directory -Recurse | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }
# list empty dir
Get-ChildItem -Directory -Recurse | Where-Object { $_.GetFiles().Count -eq 0 } | Where-Object { $_.GetDirectories().Count -eq 0 } | ForEach-Object { $_.FullName }

2021-03-20 haven't studied yet which is more proper.

List File Creation Time

# list file with creation time
Get-ChildItem "c:/Users/john/Pictures/ShareX" | Sort-Object CreationTime | Format-Table Name, CreationTime
# list files whose creation date is greater than x
Get-ChildItem "c:/Users/john/Pictures/ShareX" | Where-Object { $_.CreationTime -gt [datetime]"2014/05/28" } | Sort-Object CreationTime | Format-Table Name, CreationTime

delete file

find . -name "*~" -delete
Get-ChildItem -Recurse -Include *~ | Remove-Item

List Files that Contains Search Text

grep myRegex *html
select-string *html -pattern myRegex -CaseSensitive

Compare Files

# compare if 2 files are the same. Any binary file
cmp f1 f2
# show difference of 2 text files
diff f1 f2
# compare if 2 files are the same and or show their differences.
diff (cat f1) (cat f2)

# full syntax
Compare-Object (Get-Content f1) (Get-Content f2)

diff, sort, get column etc

sort
Sort-Object
uniq
sort -Unique
wc
Measure-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)

PowerShell

How-to

Advanced

Script Examples