5.1 KiB
PowerShell Scripting Crash Course
After having followed the README.md, .ps1
scripts in the same folder as $PROFILE.CurrentUserAllHosts
will be available in $env:PATH
globally:
# Open the $PROFILE folder
explorer (Split-Path -Parent $PROFILE.CurrentUserAllHosts)
codium (Split-Path -Parent $PROFILE.CurrentUserAllHosts)
# Or start directly editing a script
notepad "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1"
codium "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1"
variables
PowerShell is as case-insensitive as possible. This means $MyInvocation
works just the same as $mYinVoCAtiOn
among other things. Coming from a Unix background and preferring some consistency I like to pretend it is case-sensitive so in this repo I'll strive for that but it's worth keeping in mind. Especially since the Microsoft docs use case a lot in variable and cmdlet names.
Variables use $
to distinguish themselves and can be set with =
$a_variable = "this one is a string"
$interpolate_a_string = "like this: $a_variable"
booleans
There are a few constants to know: $true
, $false
, $null
. Empty strings (""
, ''
), $null
and 0
evaluate as $false
if and while
There are a handful of comparison operators which can be used with if
or while
like this:
if ($val -eq "y") {
Write-Host "value is a y"
}
else {
Write-Host "value is not a y"
}
while ($num -lt 50) {
$num = $num + 1
}
param
At the beginning of scripts and functions param
list can be specified like this
param(
[string]$name = "Anonymous",
[int]$age,
[switch]$silent
)
There are a lot of different powers behind the square bracket syntax, including custom validation, but these are the basics. Use [parameter(mandatory)]
to require one otherwise they are optional. The above param block could be used in a script like this:
# Name-And-Age.ps1
if ($silent) { exit }
Write-Host "Hello $name"
if ($age) {
Write-Host "according to the -age you provided you are: $age"
}
And run like this:
Name-And-Age.ps1 -name "Rose" -age "27"
# or, arguments are also positional!
Name-And-Age.ps1 "Rose" "27"
Get-Help
You can learn about the available param
list for pretty much anything in PowerShell with the Get-Help
cmdlet.
Get-Help Get-Help
Get-Help New-Item
Get-Help Name-And-Age.ps1
functions
Functions use param
too (instead of parenthesis like other languages). The last value is the return
result, (which you can also use to return
early... or just regularly if you want)
function My-Add {
param(
[parameter(mandatory)][int]$x,
[parameter(mandatory)][int]$y,
)
$x + $y
}
foreach
Works on arrays, which can be concatenated with +
and +=
by the way.
$letterArray = "a","b","c","d"
foreach ($letter in $letterArray)
{
Write-Host $letter
}
items and paths
In PowerShell files and folders are called items. Some useful cmdlets for working with them are:
New-Item
- create a file or folderRemove-Item
- delete a file or folder- Move, Invoke, Rename etc...
Here's how to loop through files in a folder using Get-ChildItem
:
foreach ($image Get-ChildItem .\images\* -Include *.png) {
Write-Host $image.BaseName
}
There are also a handful of useful functions for dealing with path strings by the names of [Verb]-Path. Note that in PowerShell folders are container
s and files are leaf
s. Here are some examples:
# Get parent folder
Split-Path -Parent $path
# Test if a file exists. For folder use `-PathType container`
Test-Path -Path $path -PathType leaf