Shell

Basics

PowerShell          | Alias     | Bash 
--------------- |---------  |-------- 
Get-ChildItem   | gci       | ls 
Set-Location    | sl        | cd 
Push-Location   | pushd     | pushd 
Pop-Location    | popd      | popd 
New-Item        | ni        | (touch)
mkdir           | md        | mkdir 
Explorer        | start     | (open)
Remove-Item     | rm        | rm 
Move-Item       | mv        | mv 
Copy-Item       | cp        | cp 
Write-Output    | write     | echo 
Get-Content     | gc        | cat 
Select-String   | sls       | (grep)
Measure-Object  | measure   | (wc)
>               |           | > 
|               |           | | 
Get-Help        |           | man 
exit            |           | exit 

PowerShell

Control Flow

if ($var -eq 0) { echo "no" }

$_ implicit loop variable.

$files = ls
foreach ($f in $files) { cp $f ... }
ls | Foreach-Object { cp $_ ... }

Strings

$verbatim = 'no\escape'
$i = 5
"The value of `$i is $i."
"The value of $(2+3) is 5."
'As they say, "live and learn."'

Operators

File IO

cp fromPath toPath [-Recurse] # recurse to get folder contents
Join-Path path1 path2


New-Item -Path "dirPath" -ItemType Directory
mkdir "dirPath"

# find-replace (sed)
foreach ($file in $files)
{
    $c = Get-Content $file.FullName -Raw
    $c = $c -replace 'old', 'new'
    Set-Content $c -Path $file
}

Arrays

$array = @(
   1,
   2,
   3
 )

Hashtable

$table = @{
   Brand = 'Kia'
   Color = 'Red'
}

Objects

Some Linq like methods:

select 'Name' | where -FilterScript {$_.Name -like "x*"} | sort -Property 'Time' - Descending | foreach { write $_.Name "is here" }

Calling programs

$env:LOG_VAR="info"
$prog = "path/to.exe"
$flag = "something"
& $prog flag=$flag

Command line args

# Must be the first statement in your script
param (
 [string]$server = "http://defaultserver",
 [Parameter(Mandatory=$true)][string]$username,
 [string]$password = $( Read-Host "Input password, please" ),
 [switch]$force = $false
)

or

$args[0]
$args.count

.NET interop

$dt = [datetime]::now.tostring("s")

Misc

-whatif to test things out

Import-Csv .\List.csv | Format-Table
Import-Csv .\List.csv | Out-GridView

Notifications https://www.kjctech.net/4-types-of-notifications-generated-in-powershell/

CMD

@echo on # @ suppresses echo
rem Can pass args to batch file (up to 9 args)
rem Notice no space before =
set first_arg=%1
set var=value
set "var=value"
echo %var%
set "var2=concat %var%"

# temporarily add something to path
set PATH=%PATH%;new_folder\bin

rem open help documentation on command line
if /?
rem pipes output to the clipboard
pwd | clip
rem returns the path to exe if available on PATH
where exe

%errorlevel% is the exit code of the last exe

if str1==str2 command
if ERRORLEVEL 0 (
   command
)

goto end

:end

tasklist
taskill /pid <processid>

del file.txt
del /s *.ext # incl subfolders
exit 0 # exit 1 if fail

Running batch files

To run a batch file (eg.bat) from a batch file:

call Init-Args.bat
set PROGRAM=".\p.exe"

%PROGRAM% --param1=%1 --param2=%arg%

NuShell

Custom commands

def cdls [p] { cd $p; ls}

References

Powershell