3

Is there a syntax checker for conditionals in PowerShell? This code will skip the conditional

if ($templList -ne $null -and $templList.items.Length > 0) {
  $templID=$templList.items[0].id
  write-host $templList.items
}

because the '-gt' is substituted with '>'.

1 Answer 1

6

Strictly speaking, you're looking for a semantics checker, given that your code is syntactically correct.

The problem here is that while the code was formally correct, it doesn't do what you intended it to do.

PSScriptAnalyzer (PSSA) is a linter for PowerShell, and it is integrated into the PowerShell extension for Visual Studio Code.

It would catch your problem, emitting the following message:

Did you mean to use the redirection operator '>'?
The comparison operators in PowerShell are '-gt' (greater than) or '-ge' (greater or equal).


Use in Visual Studio Code:

  • Install the PowerShell extension.

  • Once installed, with your script opened for editing, you'll see the > 0 part underlined, and hovering over it shows the message as a tooltip.

  • You can see all messages for the current file in the Problems view (Ctrl-Shift-M or, via the menus, View > Problems), which incidentally, will show you that PSSA found additional potential problems with your code snippet.

  • To configure what rules to apply (which potential problems to warn about), use >PowerShell: Select PSScriptAnalyzer Rules from the command palette.

    • The rule names, such as PSAvoidGlobalVars, are mostly self-explanatory; the rules documentation describes them (without the PS prefix); and there's also a best practices topic.

    • Important:

      • After checking / unchecking (or pressing Enter on) the rules of interest, be sure to press Enter on the Confirm menu item at the top, otherwise your changes won't take effect.

      • As of v2019.11.0, these selections aren't persisted (are remembered in the current session only) - see this GitHub issue

Note that PSSA also offers automatic (re)formatting of PowerShell code (Alt-Shift-F or, via the command palette, >Format Document).


Stand-alone use:

  • Install the PSScriptAnalyzer module from the PowerShell Gallery; e.g.:

  • Pass your script's path to the Invoke-ScriptAnalyzer cmdlet to perform linting.

With your code, you'd see the following output (having been given a script named pg.ps1):


RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSPossibleIncorrectUsageOfRedirecti Warning      pg.ps1     1     Did you mean to use the redirection operator '>'? The
onOperator                                                        comparison operators in PowerShell are '-gt' (greater than)
                                                                  or '-ge' (greater or equal).
PSPossibleIncorrectComparisonWithNu Warning      pg.ps1     1     $null should be on the left side of equality comparisons.
ll
PSUseDeclaredVarsMoreThanAssignment Warning      pg.ps1     2     The variable 'templID' is assigned but never used.
s
PSAvoidUsingWriteHost               Warning      pg.ps1     3     File 'pg.ps1' uses Write-Host. Avoid using Write-Host
                                                                  because it might not work in all hosts, does not work when
                                                                  there is no host, and (prior to PS 5.0) cannot be
                                                                  suppressed, captured, or redirected. Instead, use
                                                                  Write-Output, Write-Verbose, or Write-Information.
Sign up to request clarification or add additional context in comments.

4 Comments

I have it installed in VS code with, "powershell.scriptAnalysis.enable": true; and it reports 'No problems'. However, changing '-ne' to != showed a problem.
@un-CharlieH: Interesting - are you trying with the exact same code as in your question? I definitely see the warning re > in VSCode 1.39.2, with the latest PowerShell extension 2019.9.0 and its bundled PSScriptAnalyzer 1.18.3
I have PS 6.1 installed and VS Code automatically uses it. I switched to PS 5.1 and see it now! Thanks for pointing the way.
@un-CharlieH: Glad to hear it; I do also see the warning with the latest 6.x Core version, 6.2.3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.