Skip to content

Latest commit

 

History

History
176 lines (115 loc) · 3.99 KB

PowerShellTricks.md

File metadata and controls

176 lines (115 loc) · 3.99 KB

PowerShell Tricks

This is a collection of some PowerShell tricks, especially if you come from Unix bash.

set -e

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

set -x

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

wget

This one is easy.

wget -Uri $url -OutFile $localfile

Downloading a file (wget, curl)

Invoke-WebRequest works for both windowsservercore and nanoserver.

$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest $url -OutFile $target -UseBasicParsing 

The  $ProgressPreference setting is to fix download speed as sometimes downloading with Invoke-WebRequest was poorly slow.

Only for windowsservercore, you can also use

$wc = New-Object net.webclient; $wc.Downloadfile($url, $target)

TLS 1.2

Some web sites enforce you to use TLS 1.2. You have to enable TLS 1.2 in PowerShell before the Invoke-WebRequest or WebClient call.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;

curl -u

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 )

unzip

To extract a ZIP file use this

$ProgressPreference = 'SilentlyContinue'
Expand-Archive -Path $zip -DestinationPath $dest -Force

shasum

To calculate a SHA256 sum of a file use this

((Get-FileHash $filename -Algorithm sha256).Hash

printenv

To list all environment variables use

ls env:

ls -lt

List files sorted by date, oldest files at the end

ls | sort LastWriteTime -Des

or with the full option: ls | sort LastWriteTime -Descending

ls -ltr

List files sorted by date, newest files at the end

ls | sort LastWriteTime

wc -l

Count the lines of stdin, like in ls | wc -l

ls | measure

time another command

To measure the time that a command takes, use

Measure-Command {docker run microsoft/nanoserver hostname}

time the stdout of another command

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

elapsed time of another command for each line of stdout

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

Docker commands

Delete all containers

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!

Get Windows version

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

Links

Basic networking PowerShell Cmdlets cheatsheet to replace netsh, ipconfig, nslookup and more