When you run a script, how can you be sure it did exactly what you intended? Maybe it removed a file or stopped a service. But did it do so flawlessly across every environment, user session, or machine it touched? If your script impacts dozens—testing with Pester is your best bet.
Pester, the powerful PowerShell module, helps you write automated tests using a domain-specific language (DSL). In this tutorial, you’ll learn to eliminate the guesswork and create a rock-solid process to validate your scripts.
Buckle up and let Pester boost your scripting confidence!
Installing and Configuring Pester
Pester ensures your script’s actions match your expectations in any environment. Testing with Pester is an essential skill for managing infrastructure with PowerShell.
But first, we need to install Pester to build some tests around a script that provisions a server to see how Pester tests work.
You can download the Pester
module from the PowerShell Gallery:
Install-Module Pester
If you encounter a warning, it might be because Windows 10 (and later) clients have an older version of Pester installed by default. For example, version 3.4.0 is often pre-installed.
Ensure you remove the old version to avoid conflicts:
$module = "C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0" takeown /F $module /A /R icacls $module /reset icacls $module /grant "*S-1-5-32-544:F" /inheritance:d /T Remove-Item -Path $module -Recurse -Force -Confirm:$false
This script takes ownership of the Pester 3.4.0 folder, updates permissions, and removes it.
Install Pester’s latest version by appending the -Force
parameter:
Install-Module Pester -Force
You might see another warning regarding the new version’s signature. Microsoft signed the pre-installed version, whereas Pester’s maintainer, Jakub, signed the latest version.
Verify the installed version with the following command:
Get-Module -Name Pester -ListAvailable
Creating a Pester Test File
Now that Pester is installed let’s create some tests. For demonstration purposes, assume a script is tasked with installing the IIS Windows feature.
Create a PowerShell script named ServerProvisioning.Tests.ps1 in your desired directory; let’s say it’s in your ~\Documents folder.
Next, execute Pester tests with the Invoke-Pester
cmdlet:
cd ~\Documents Invoke-Pester
Running this command without any tests available results in no tests being executed.
Let’s add some tests using Pester’s hierarchical block structure, where:
describe
represents a category of tests, such as a script’s major functionality.context
optionally organizes tests into subcategories.it
defines individual tests.
describe 'IIS' { context 'Windows features' { it 'installs the Web-Server Windows feature' { } } } describe 'RegistryTweaks' { } describe 'SoftwareInstalls' { }
Adding and Running a Test
Imagine a script that provisions a server but misses a crucial step, like enabling a required feature. If left unchecked, this oversight can cause cascading issues in production.
Suppose the server provisioning script has run, and the task is to confirm that the Web-Server
feature is installed. If so, perform a manual check.
Run the following command to manually check if the Web-Server
feature (IIS) is installed on the remote server.
Invoke-Command -ComputerName SRV1 -ScriptBlock { (Get-WindowsFeature -ComputerName SRV1 -Name Web-Server).Installed }
This command returns True
if the feature is installed and False
otherwise.
Now, add the following to the it
block to automate this test. The should
operator with the -BeTrue
condition asserts that the feature is installed.
describe 'IIS' { context 'Windows features' { it 'installs the Web-Server Windows feature' { Invoke-Command -ComputerName SRV1 -ScriptBlock { (Get-WindowsFeature -ComputerName SRV1 -Name Web-Server).Installed } | should -BeTrue } } }
Finally, rerun the test:
cd ~\documents Invoke-Pester
As expected, the test will fail if the feature isn’t installed.
VS Code has some great Pester integration, too. Instead of running Invoke-Pester
, you can click the Run Tests
item on individual tests directly in VS Code. This action invokes specific tests.
Conclusion
In this tutorial, you learned how to use Pester to automate testing for your PowerShell scripts. You’ve covered the foundational steps to validate your scripts effectively. These steps include installing and configuring Pester, creating a test file, and writing your first automated test.
Build on this foundation by exploring how Pester can handle more advanced scenarios, such as testing APIs, mocking commands, or running pre- and post-deployment validations.
As you integrate Pester into your workflow, you’ll improve your scripts and gain trust in their reliability—confidently scale your automation efforts!