While the accepted answer correctly explains why Get-Command -Syntax and even Invoke-ScriptAnalyzer do not catch logical errors like keyword typos (e.g., ifTypo), the direct answer to "Is there any other Command which can perform full syntax check?" is yes.
The definitive way to perform a pure syntax check is to use PowerShell's own internal parser, available via the .NET class [System.Management.Automation.Language.Parser].
This method will not execute the script, but it will catch any true parsing errors, such as mismatched brackets ({}), incomplete commands, or invalid language constructs.
The Solution: A Reusable Syntax-Checking Function
Here is a simple, reusable PowerShell function that wraps the parser's functionality. You can use this in your console application or any automation pipeline.
<#
.SYNOPSIS
Validates the syntax of a PowerShell script file without executing it.
.DESCRIPTION
This function uses the internal .NET Parser class to perform a full syntax
check on a specified .ps1 file. It does not execute any of the script's logic.
It returns any parsing errors it finds. If no errors are found, it returns nothing.
.PARAMETER Path
The path to the PowerShell script file (.ps1) to validate.
.EXAMPLE
PS C:\> Test-PSScriptSyntax -Path "C:\Scripts\GoodScript.ps1"
# This command will produce no output if the syntax is valid.
.EXAMPLE
PS C:\> $errors = Test-PSScriptSyntax -Path "C:\Scripts\BadScript.ps1"
PS C:\> if ($errors) {
>> Write-Warning "Syntax errors were found:"
>> $errors | ForEach-Object { $_.Message }
>> }
WARNING: Syntax errors were found:
Missing closing '}' in statement block or type definition.
.OUTPUTS
System.Management.Automation.Language.ParseError[]
#>
function Test-PSScriptSyntax {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$Path
)
# Ensure the file exists before trying to parse it.
if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) {
throw "File not found: $Path"
}
$scriptContent = Get-Content -LiteralPath $Path -Raw
$errors = $null
# Use the internal parser to check the script content.
# The [void] cast suppresses the AST (Abstract Syntax Tree) output,
# so we only capture the errors.
[void][System.Management.Automation.Language.Parser]::ParseInput($scriptContent, [ref]$null, [ref]$errors)
# Return the collection of errors. If there are no errors, this returns nothing.
return $errors
}
How to Use This Function
Save the function above into your PowerShell profile or dot-source it in your console application.
Run it against your scripts.
Example with a valid script:
# This will return nothing, indicating success.
Test-PSScriptSyntax -Path 'D:\powershell\validScript.ps1'
Example with a script that has a missing }:
# This will return one or more ParseError objects.
$syntaxErrors = Test-PSScriptSyntax -Path 'D:\powershell\brokenScript.ps1'
if ($syntaxErrors) {
Write-Host "Found $($syntaxErrors.Count) syntax error(s):"
$syntaxErrors.Message
}
Output:
Found 1 syntax error(s):
Missing closing '}' in statement block or type definition.
Answering Your Original Questions
With this new understanding, we can answer your specific questions:
How efficient is Get-Command -syntax for a syntax check?
- It is not efficient or reliable. Its purpose is to show a command's parameters, and it only performs a surface-level parse. It will not catch most syntax errors.
Does it only validate the syntax of PowerShell cmdlets, functions, and aliases?
- It doesn't truly "validate" anything. It inspects a script to find its
param() block to display the syntax diagram. The parsing it does is a side effect and is very permissive.
Is there any other Command which can perform full syntax check?
- Yes. The
Test-PSScriptSyntax function provided above uses the official Parser class and is the correct way to perform a pure syntax check.
Why Your Original Script Still Passes a Syntax Check
It's crucial to understand that your original script with ifTypo and FunctionTypo is syntactically valid according to the PowerShell language rules.
This is a runtime error (command not found), not a parse-time syntax error.
To catch these kinds of issues, you need different tools:
For Undeclared Variables ($log_dir): The best practice is to put Set-StrictMode -Version Latest at the top of your scripts. This will cause the script to fail at runtime if it tries to use an undeclared variable.
For Typos and Best Practices: As others have mentioned, Invoke-ScriptAnalyzer is the tool for this. It is a "linter" that goes beyond basic syntax to find potential bugs, style issues, and common problems. It is the engine used by VS Code to provide real-time feedback.
Invoke-ScriptAnalyzer?Get-Commanddoesn't validate anything - it only discovers thingsGet-Command -Syntaxis to show the syntax diagram (usage information), but to do so the script must be parsed, so as a side effect syntax errors do surface.