This is a collection of some PowerShell tricks, especially if you come from Unix bash.
In bash I often want to abort the script if an exit code of a program is not zero or another error occurs. The bash equivalent to set -e
$ErrorActionPreference = 'Stop'
The set +e
equivalent is
$ErrorActionPreference = 'Continue'
You can use this in a multi line RUN
command in Dockerfiles for Windows to abort on the first error:
- Windows:
RUN powershell -Command $ErrorActionPreference = 'Stop'; fail; success
- Linux:
RUN fail && success
In bash I sometimes want to debug the script while it is running. The bash set -x
flag shows all lines as well as the @echo on
in cmd shell, the Powershell equivalent is
Set-PSDebug -Trace 1
This one is easy.
wget -Uri $url -OutFile $localfile
Invoke-WebRequest
works for both windowsservercore and nanoserver.
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest $url -OutFile $target -UseBasicParsing
Only for windowsservercore, you can also use
$wc = New-Object net.webclient; $wc.Downloadfile($url, $target)
To download a file with BasicAuth use this
$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.Networkcredential($user, $pass)
$WebClient.DownloadFile( $url, $localfile )
To extract a ZIP file use this
$ProgressPreference = 'SilentlyContinue'
Expand-Archive -Path $zip -DestinationPath $dest -Force
To calculate a SHA256 sum of a file use this
((Get-FileHash $filename -Algorithm sha256).Hash
To list all environment variables use
ls env:
List files sorted by date, oldest files at the end
ls | sort LastWriteTime -Des
or with the full option: ls | sort LastWriteTime -Descending
List files sorted by date, newest files at the end
ls | sort LastWriteTime
Count the lines of stdin, like in ls | wc -l
ls | measure
To measure the time that a command takes, use
Measure-Command {docker run microsoft/nanoserver hostname}
To prepend the timestamp for each stdout line of another command use this
filter timestamp {"$(Get-Date -Format o): $_"}
dir | timestamp
docker-compose up -d 2>&1 | timestamp
To prepend the time elapsed time to each line of stdout ue this
filter addtime {"$((new-timespan -start $start -end (Get-Date)).TotalSeconds): $_"}
$start=(Get-Date) ; dir | addtime
$start=(Get-Date) ; docker-compose up -d 2>&1 | addtime
From time to time playing with containers you might want to just delete all containers. This one is really easy and exactly the way as on Linux/Mac:
docker rm -vf $(docker ps -qa)
Surprise!
PS C:\Users\vagrant> $(gp "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx
14393.447.amd64fre.rs1_release_inmarket.161102-0100
PS C:\Users\vagrant> winver