Skip to content
mnaoumov.dev
Go back

PowerShell script best practices

I would like to share some ideas that I believe is best practices in PowerShell development

Script Prefixing

In the beginning of all my scripts I add

#requires -version 2.0

[CmdletBinding()]
param
(
)

$script:ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$PSScriptRoot = $MyInvocation.MyCommand.Path | Split-Path

Line 1 - enforcing PowerShell version

Line 3 - Automatically implement the standard -Verbose, -Debug, -ErrorAction, -ErrorVariable, -Confirm, and -? arguments for you and maybe some other minor stuff.

Line 8 - Enforces all errors to become terminating unless you override with per-command -ErrorAction parameters or wrap in a try-catch block script: prefix is used to fix some known issue with PSv3CTP2 ISE bug

Line 9 - Ensures you only refer to variables that exist (great for typos) and enforces some other “best-practice” coding rules.

Line 10 - This gets the absolute path of the folder containing the script that is running. This is useful when referring to other files relative to the script without relying on the current working directory. The variable name is chosen as such because this is the name of the variable PowerShell gives you for free when you write a module thereby making future refactoring to a module easier. Also I would like to mention that this variable became automatically set in PowerShell 3 for ps1 scripts, so Line 10 is not needed if you are targetting PowerShell 3 only.

Most of these descriptions were stolen word-by-word from Jason Stangroome (http://blog.codeassassin.com/)

Readability

I think readability of the scripts is very important. So

See here also


Share this post on:

Previous Post
Useful git hooks - Part 3
Next Post
WTF Method overloads resolution in PowerShell