header image

Windows 10 and PSScriptAnalyzer

Posted by: | August 10, 2015 Comments Off on Windows 10 and PSScriptAnalyzer |

if you were wondering where the PSScriptanalyzer module was in Windows 10 RTM  – its not there.  Its been moved out of core PowerShell and is now available on the PSGallery.

£> Find-Module *Script*

Version    Name                                Repository
——-    —-                                ———-
0.1        nScriptAnalyzerRules                PSGallery
1.0.2      PSScriptAnalyzer                    PSGallery    ScriptBrowser                       PSGallery    ScriptCop                           PSGallery    ScriptCS                            PSGallery
1.0        ScriptTransforms                    PSGallery
1.0        SQLInvokeScripts                    PSGallery


You can install it from the gallery

£> Install-Module -Name PSScriptAnalyzer


You are installing the module(s) from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet.
Are you sure you want to install software from ‘https://www.powershellgallery.com/api/v2/’?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y


By default the gallery isn’t a trusted source so you’ll need to confirm the install.  I’d recommend NOT making PSGallery a trusted source. That designation should be reserved for your internal repositories.


The module has 2 cmdlets

£> Get-Command -Module PSScriptAnalyzer

CommandType     Name
———–     —-
Cmdlet          Get-ScriptAnalyzerRule
Cmdlet          Invoke-ScriptAnalyzer


You can view the rules currently available:

£> Get-ScriptAnalyzerRule | Format-Table RuleName, Severity -AutoSize

RuleName                                          Severity
——–                                          ——–
PSAvoidUsingCmdletAliases                          Warning
PSAvoidDefaultValueSwitchParameter                 Warning
PSAvoidUsingEmptyCatchBlock                        Warning
PSAvoidGlobalVars                                  Warning
PSAvoidInvokingEmptyMembers                        Warning
PSAvoidUsingPositionalParameters                   Warning
PSReservedCmdletChar                               Warning
PSReservedParams                                   Warning
PSAvoidShouldContinueWithoutForce                  Warning
PSAvoidUsingDeprecatedManifestFields               Warning
PSProvideDefaultParameterValue                     Warning
PSAvoidUninitializedVariable                       Warning
PSAvoidUsingUserNameAndPassWordParams                Error
PSAvoidUsingComputerNameHardcoded                    Error
PSAvoidUsingConvertToSecureStringWithPlainText       Error
PSAvoidUsingInternalURLs                       Information
PSAvoidUsingInvokeExpression                       Warning
PSAvoidUsingPlainTextForPassword                   Warning
PSAvoidUsingWMICmdlet                              Warning
PSAvoidUsingWriteHost                              Warning
PSUseOutputTypeCorrectly                       Information
PSMissingModuleManifestField                       Warning
PSPossibleIncorrectComparisonWithNull              Warning
PSProvideCommentHelp                           Information
PSUseApprovedVerbs                                 Warning
PSUseCmdletCorrectly                               Warning
PSUseDeclaredVarsMoreThanAssigments                Warning
PSUsePSCredentialType                              Warning
PSShouldProcess                                    Warning
PSUseShouldProcessForStateChangingFunctions        Warning
PSUseSingularNouns                                 Warning
PSDSCDscTestsPresent                           Information
PSDSCDscExamplesPresent                        Information
PSDSCUseVerboseMessageInDSCResource            Information
PSDSCUseIdenticalMandatoryParametersForDSC           Error
PSDSCUseIdenticalParametersForDSC                    Error
PSDSCStandardDSCFunctionsInResource                  Error
PSDSCReturnCorrectTypesForDSCFunctions         Information


You can also get a description of the rule. Its worth reading through the rules to see what is considered best practice by the authors of this module!

This simple script should trigger a few rules Smile

Write-Host ‘getting data’
gwmi Win32_computersystem


£> Invoke-ScriptAnalyzer -Path C:\TestScripts\testsa.ps1 | fl

RuleName : PSAvoidUsingWriteHost
Severity : Warning
Line     : 1
Column   : 1
Message  : File ‘testsa.ps1’ uses Write-Host. This is not recommended because it may not work in some hosts or there  may even be no hosts at all. Use Write-Output instead.

RuleName : PSAvoidUsingPositionalParameters
Severity : Warning
Line     : 2
Column   : 1
Message  : Cmdlet ‘gwmi’ has positional parameter. Please use named parameters instead of positional parameters when  calling a command.

RuleName : PSAvoidUsingPositionalParameters
Severity : Warning
Line     : 1
Column   : 1
Message  : Cmdlet ‘Write-Host’ has positional parameter. Please use named parameters instead of positional parameters when calling a command.

RuleName : PSAvoidUsingCmdletAliases
Severity : Warning
Line     : 2
Column   : 1
Message  : ‘gwmi’ is an alias of ‘Get-WmiObject’. Alias can introduce possible problems and make scripts hard to  maintain. Please consider changing alias to its full content.


Nice simple way to test your code for best practices and avoid some of the major issues that make scripts difficult to read and maintain

under: PowerShell v5

Comments are closed.