Skip to content
Categories:

Validate Powershell Code with Pester

Post date:
Author:

Example on how you can verify that code in a Powershell script is valid Powershell code using Pester

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Describe 'Valid Powershell code' {

    $folder = [folder path]
    $files = Get-ChildItem $Folder -Recurse | Where-Object name -like '*.ps1'
    foreach ($file in $files) {
      Context "Test file : $file" {
        It "$file has valid PowerShell code" {
          $psFile = Get-Content $file.FullName
          $errors = $null
          $null = [System.Management.Automation.PSParser]::Tokenize($psFile, [ref]$errors)
             
          $errors.count | Should Be 0
        }
      }
    }
  }