Examples of bash commands and how to do the same thing in PowerShell
Echo
Write text to the screen.
-
Bash
-
PowerShell
Dirty version:
echo 'Hello, World!'
Execute a Script
Save and execute a script with bash and PowerShell.
-
Bash
Assume that echo1.sh
has the following contents.
You would the execute it with the following command line.
-
PowerShell
Assume echo1.ps1
has the following contents.
You would then execute it with the following command line.
Comments are the same in PowerShell and bash.
-
Bash
-
PowerShell
Multiline comments are different between bash and PowerShell.
-
Bash
-
PowerShell
While Loop
-
Bash
-
PowerShell
For Loop
-
Bash
-
PowerShell
-
Bash
-
PowerShell
If Statement
-
Bash
-
PowerShell
If with And Logic
-
Bash
-
PowerShell
If with Or Logic
-
Bash
-
PowerShell
If Else Statements
-
Bash
-
PowerShell
Case Statement
-
Bash
-
PowerShell
Command Line Arguments
-
Bash
-
PowerShell
Named Command Line Arguments
-
Bash
-
PowerShell
Concatenating Strings
-
Bash
-
PowerShell
Substring of a String
-
Bash
-
PowerShell
Functions
-
Bash
-
PowerShell
Functions with Parameters
-
Bash
-
PowerShell
Use the Return value of a Function
-
Bash
-
PowerShell
Make a Directory
-
Bash
-
PowerShell
Dirty version:
mkdir $newdir
Make Directory if it doesn’t exist
-
Bash
-
PowerShell
Dirty version:
$newdir = Read-Host "Enter directory name"
if (gi $newdir -ea si)
{
echo "Directory exists"
} else {
mkdir $newdir
}
Read a File
-
Bash
-
PowerShell
Delete a File
-
Bash
-
PowerShell
Dirty version:
$fn = Read-Host "Enter a file to remove"
rm $fn
Append to a File
-
Bash
-
PowerShell
Wait for a Process
-
Bash
-
PowerShell
Sleep
-
Bash
-
PowerShell
Dirty version:
sleep 5
Write-Host "Completed"
Download Files
-
Bash
-
PowerShell
Dirty version:
iwr http://www.het.brown.edu/guide/UNIX-password-security.txt -o newname.txt
Search a File for a String
-
Bash
-
PowerShell
Sources:
Related:
Shell, Bash - The Linux command line
Tags:
PowerShell - A command-line shell and scripting language to manage Windows system and automate administrative tasks