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
if ($var -eq 0) { echo "no" }
$_ implicit loop variable.
$files = ls
foreach ($f in $files) { cp $f ... }
ls | Foreach-Object { cp $_ ... }$verbatim = 'no\escape'
$i = 5
"The value of `$i is $i."
"The value of $(2+3) is 5."
'As they say, "live and learn."'$( ) Subexpression operator: means execute this first
and then treat the result like a variable... Range operator e.g. 10..20:: Static member operator
e.g. [datetime]::nowcp 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
}$array = @(
1,
2,
3
)
$table = @{
Brand = 'Kia'
Color = 'Red'
}
Some Linq like methods:
select 'Name' | where -FilterScript {$_.Name -like "x*"} | sort -Property 'Time' - Descending | foreach { write $_.Name "is here" }$env:LOG_VAR="info"
$prog = "path/to.exe"
$flag = "something"
& $prog flag=$flag
# 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$dt = [datetime]::now.tostring("s")-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/
@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 failTo run a batch file (eg.bat) from a batch file:
CALL eg.bat: will start it in the same window and the
called batch has access to the same variable context. It can also change
variables which affects the caller.START eg.bat: will create a new cmd.exe for the called
batch, and without /b it will open a new window. As it’s a new context,
variables can’t be shared.call Init-Args.bat
set PROGRAM=".\p.exe"
%PROGRAM% --param1=%1 --param2=%arg%Custom commands
def cdls [p] { cd $p; ls}
Powershell