Skip to content

check-ps1-file

Checks PowerShell file(s) for validity

#Requires -Version 5.1

param([string]$filePattern = "")

try {
	if ($filePattern -eq "" ) { $path = Read-Host "Enter the file pattern to the PowerShell file(s)" }

	$files = Get-ChildItem -path "$filePattern" -attributes !Directory
	foreach ($file in $files) {
		$syntaxError = @()
		[void][System.Management.Automation.Language.Parser]::ParseFile($file, [ref]$null, [ref]$syntaxError)
		if ("$syntaxError" -ne "") { throw "$syntaxError" }
		"✅ Valid PowerShell in $($file.Name)"
	}
	exit 0
} catch {
throw
}

Specifies the file pattern to the PowerShell file(s)

An interactive directory of PowerShell scripts.