10 Quick tips for writing better PowerShell scripts

If you think your friends/network would find this useful, please share it with them – I’d really appreciate it.

Want to know best practices for creating PowerShell scripts? In this post, I give you some of the tips and practices I use when developing scripts. Something to keep in mind when creating scripts is: a script saves time, automation can mean freedom where you get time to do higher-value tasks, less time on boring aspects of the job, and more time on valuable tasks that benefits the organization.

Here are 10 quick tips and practices to keep in mind when developing PowerShell scripts.

Tip #1

When creating scripts always try to parameterize as much as you can to avoid hard-coding. Especially avoid hard-coding domain names, passwords, credentials, or anything that can expose information about your environment, username, and password. The script should be generic enough so that you could post on GitHub without exposing any of the information above.

Tip #2

Don’t use aliases in your script – This makes it more readable, considering you often make the script ones and then leave them. This is to make it easier for yourself and others by avoiding using aliases. For instance, a simple command like listing a specified folder in a directory with and without aliases. Which one is easier to understand?

# With aliases
PS C:\>gci | ? { $_.Name -like "Windows" }

# Without aliases
PS C:\>Get-ChildItem | Where-Object { $_.Name -like "Windows" }

Tip #3

Always use the full cmdlet and parameter names, it’s much harder to troubleshoot and debug scripts using a lot of encrypted aliases, rather than keeping it simple and consistent.

Tip #4

Write clean and formatted code, by using Visual Studio Code with the PowerShell plugin, to clean up the code. A shortcut in Visual Studio Code is pressing CTRL + Alt +F. This shortcut formats the code and makes it more readable, and lets you focus on the action of the tool rather than formatting.

Tip #5

An important tip is always to keep in mind when writing your script, write for the next person. This person can be you and do yourself a favor and make it as easy as possible. You most likely write it once, so make sure to write it great the first time.

Tip #6

Use double quotations when using cmdlets like Write-Verbose, Write-Host and Write-Output, when you combine strings and variables, with double quotations you can output strings and variables without having to concatenate.

# Double quotation without concatination, will output string and variable
$World = "World"
PS C:>Write-Output "Hello $World"

# Double quotation without concatination, will also output string andvariable
$World = "World"
PS C:>Write-Output "Hello" + $World

# Single quotation without concatination, will output a string only
$World = "World"
# With aliases
PS C:\>gci | ? { $_.Name -like "Windows" }

# Without aliases
PS C:\>Get-ChildItem | Where-Object { $_.Name -like "Windows" }

Tip #7

Use Write-Output instead of Write-Host when working with objects.

Don’t use Write-Host if you plan to make modifications to the object you write to the console, this cmdlet will only output plain text, while Write-Output keeping the object in place, and you can make modifications to the object. The Write-Host is useful when writing status updates when scripts are run by using it’s a parameter -Foregroundcolor where you add colors, for instance; red for errors, green for success, yellow for warning.

Tip #8

When you create and work with functions, remember to run the script to refresh the memory in the PowerShell console. In PowerShell ISE and Visual Studio Code, you can run the script by highlighting the code and pressing F8, or you can run it from the console using dot source, for instance . C:\scriptexample.ps1. If you don’t see any changes in the action of the script after modifying it, it most likely is because the PowerShell memory isn’t updated.

Tip #9

When creating a function use the attribute [CmdletBindings()] this is a special attribute that creates an advanced function with Cmdlet capabilities and gives access to many neat features, like Write-Verbose, Debug and ErrorAction.

Tip #10

The last tip in this post, if you want to verify and test your code, you can use the built-in PowerShell module Pester. This is a unit test module for testing PowerShell scripts, in the blog post How to convert IDN domain to ACE encoding with PowerShell you can see an example of how it looks and can be used.