Examples of bash commands and how to do the same thing in PowerShell
Echo
Write text to the screen.
-
Bash
echo "Hello, World!" echo -e "Hello, \t World!"
-
PowerShell
Write-Output 'Hello, World!' Write-Output "Hello, `t World!"
Dirty version:
echo 'Hello, World!'
Execute a Script
Save and execute a script with bash and PowerShell.
-
Bash
Assume thatecho1.sh
has the following contents.echo "Hello, World!"
You would the execute it with the following command line.
bash echo1.sh
-
PowerShell
Assumeecho1.ps1
has the following contents.Write-Output "Hello, World!"
You would then execute it with the following command line.
pwsh echo1.ps1
Comments
Comments are the same in PowerShell and bash.
-
Bash
# My Comment!
-
PowerShell
# My Comment!
Multiline Comments
Multiline comments are different between bash and PowerShell.
-
Bash
: 'Multi line comment' echo "42"
-
PowerShell
<# Multi line comment #> Write-Output "42"
While Loop
-
Bash
valid=true count=1 while [ $valid ] do echo $count if [ $count -eq 5 ]; then break fi ((count++)) done
-
PowerShell
$count = 0 while ($true) { Write-Host $count if ($count -eq 5) { break } $count++ }
For Loop
-
Bash
for (( counter=10; counter>0; counter-- )) do echo -n "$counter " done
-
PowerShell
for ($counter = 10; $counter -gt 0; $counter--) { Write-Host $counter }
Get User Input
-
Bash
echo "Enter Your Name" read name echo "Welcome, $name!"
-
PowerShell
$name = Read-Host "Enter Your Name" Write-Host "Welcome, $name!"
If Statement
-
Bash
n=10 if [ $n -lt 10 ]; then echo "It is a one digit number" else echo "It is a two digit number" fi
-
PowerShell
$n = 10 if ($n -lt 10) { Write-Host "It is a one digit number" } else { Write-Host "It is a two digit number" }
If with And Logic
-
Bash
echo "Enter username" read username echo "Enter password" read password if [[ ( $username == "admin" && $password == "secret" ) ]]; then echo "valid user" else echo "invalid user" fi
-
PowerShell
$UserName = Read-Host "Enter username" $Password = Read-Host "Enter password" if ($username -eq 'admin' -and $password -eq 'secret') { Write-Host 'valid user' } else { Write-Host 'invalid user' }
If with Or Logic
-
Bash
echo "Enter any number" read n if [[ ( $n -eq 15 || $n -eq 45 ) ]] then echo "You won the game" else echo "You lost the game" fi
-
PowerShell
[int]$n = Read-Host "Enter any number" if ($n -eq 15 -or $n -eq 45) { Write-Host "You won the game" } else { Write-Host "You lost the game" }
If Else Statements
-
Bash
echo "Enter your lucky number" read n if [ $n -eq 101 ]; then echo "You got 1st prize" elif [ $n -eq 510 ]; then echo "You got 2nd prize" elif [ $n -eq 999 ]; then echo "You got 3rd prize" else echo "Sorry, try for the next time" fi
-
PowerShell
[int]$n = Read-Host "Enter your lucky number" if ($n -eq 101) { Write-Host "You got 1st prize" } elseif ($n -eq 510) { Write-Host "You got 2nd prize" } elseif ($n -eq 999) { Write-Host "You got 3rd prize" } else { Write-Host "Sorry, try for the next time" }
Case Statement
-
Bash
echo "Enter your lucky number" read n case $n in 101) echo "You got 1st prize" ;; 510) echo "You got 2nd prize" ;; 999) echo "You got 3rd prize" ;; *) echo "Sorry, try for the next time" ;; esac
-
PowerShell
[int]$n = Read-Host "Enter your lucky number" switch($n) { 101 { Write-Host "You got 1st prize" } 510 { Write-Host "You got 2nd prize" } 999 { Write-Host "You got 3rd prize" } default { Write-Host "Sorry, try for the next time" } }
Command Line Arguments
-
Bash
echo "Total arguments : $#" echo "1st Argument = $1" echo "2nd argument = $2"
-
PowerShell
Write-Host "Total arguments: $($args.Length)" Write-Host "1st Argument: $($args[0])" Write-Host "2nd Argument: $($args[1])"
Named Command Line Arguments
-
Bash
for arg in "$@" do index=$(echo $arg | cut -f1 -d=) val=$(echo $arg | cut -f2 -d=) case $index in X) x=$val;; Y) y=$val;; *) esac done ((result=x+y)) echo "X+Y=$result"
-
PowerShell
param([int]$X, [int]$Y) $Result = $X + $Y Write-Host "X+Y=$Result"
Concatenating Strings
-
Bash
string1="Linux" string2="Hint" echo "$string1$string2" string3=$string1+$string2 string3+=" is a good tutorial blog site" echo $string3
-
PowerShell
$string1 = "Ironman" $string2 = "Software" Write-Host "$string1$string2" $string3 = $string1+$string2 $string3 +=" makes good software" Write-Host $string3
Substring of a String
-
Bash
Str="Learn Linux from LinuxHint" subStr=${Str:6:5} echo $subStr
-
PowerShell
$Str = 'Learn PowerShell from Ironman Software' $subStr = $Str.Substring(6, 10) Write-Host $subStr
Functions
-
Bash
function F1() { echo 'I like bash programming' } F1
-
PowerShell
function F1 { Write-Host "I like PowerShell programming" } F1
Functions with Parameters
-
Bash
Rectangle_Area() { area=$(($1 * $2)) echo "Area is : $area" } Rectangle_Area 10 20
-
PowerShell
function RectangleArea { $Area = $Args[0] * $Args[1] Write-Host "Area is: $Area" } RectangleArea 10 20
Use the Return value of a Function
-
Bash
function greeting() { str="Hello, $name" echo $str } echo "Enter your name" read name val=$(greeting) echo "Return value of the function is $val"
-
PowerShell
function Greeting { "Hello, $Name" } $Name = Read-Host "Enter your name" $Val = Greeting Write-Host "Return value of the function is $val"
Make a Directory
-
Bash
echo "Enter directory name" read newdir `mkdir $newdir`
-
PowerShell
$newdir = Read-Host "Enter directory name" New-Item $newdir -ItemType Directory
Dirty version:
mkdir $newdir
Make Directory if it doesn’t exist
-
Bash
echo "Enter directory name" read ndir if [ -d "$ndir" ] then echo "Directory exist" else `mkdir $ndir` echo "Directory created" fi
-
PowerShell
$newdir = Read-Host "Enter directory name" if (Test-Path $newdir) { Write-Host "Directory exists" } else { New-Item $newdir -ItemType Directory }
Dirty version:
$newdir = Read-Host "Enter directory name"
if (gi $newdir -ea si)
{
echo "Directory exists"
} else {
mkdir $newdir
}
Read a File
-
Bash
file='book.txt' while read line; do echo $line done < $file
-
PowerShell
Get-Content 'book.txt'
Delete a File
-
Bash
echo "Enter filename to remove" read fn rm -i $fn
-
PowerShell
$fn = Read-Host "Enter a file to remove" Remove-Item $fn
Dirty version:
$fn = Read-Host "Enter a file to remove"
rm $fn
Append to a File
-
Bash
echo "Before appending the file" cat book.txt echo "Learning Laravel 5">> book.txt echo "After appending the file" cat book.txt
-
PowerShell
Write-Host "Before appending the file" Get-Content book.txt "Learning Laravel 5" >> book.txt Write-Host "After appending the file" Get-Content book.txt
Wait for a Process
-
Bash
echo "Wait command" & process_id=$! wait $process_id echo "Exited with status $?"
-
PowerShell
Start-Process notepad -Wait Get-Process -Id $ProcessId | Wait-Process
Sleep
-
Bash
echo “Wait for 5 seconds” sleep 5 echo “Completed”
-
PowerShell
Write-Host "Wait for 5 seconds" Start-Sleep 5
Dirty version:
sleep 5
Write-Host "Completed"
Download Files
-
Bash
curl -o newname.txt http://www.het.brown.edu/guide/UNIX-password-security.txt
-
PowerShell
Invoke-WebRequest http://www.het.brown.edu/guide/UNIX-password-security.txt -OutFile .\newname.txt
Dirty version:
iwr http://www.het.brown.edu/guide/UNIX-password-security.txt -o newname.txt
Search a File for a String
-
Bash
error log.txt # Search a file grep error * # Search a directory grep -r error * # Recursively search a directory grep -l error * # List matching files grep -c error * # Measure number of objects grep -C 2 error * # Display lines before and after
-
PowerShell
Select-String error log.txt # Search a file Select-String error * # Search a Directory Get-ChildItem * -Recurse | Select-String error # Recursively search a directory Select-String error * | Select-Object path # List matching files Select-String error * | Measure-Object # Count Matches Select-String error * # Display lines before and after
Sources:
- 2021-12-09: Bash vs PowerShell Cheat Sheet
- 2021-12-09: 30 Bash Script Examples
Related: Shell, Bash - The Linux command line