You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature request: Mark specific lines or blocks to ignore for specific rules #849
Like pragma in c# can disable a warning for a line and then enable it again.
As an example, I've got a few variables that are used to within Invoke-Expression's, and the script analyzer flags them as unused variables but they are actually used. I dont want to ignore the rule for the entire script, just these few variables.
Andrew Stanton (@StingyJack) You could define a local function and suppress it on this local one only (you can define even define functions inside functions to limit its scope).
Because of the way scoping works in PowerShell (a child scope inherits the variables from its parent), you do not even need to pass in the variables in theory (although personally I am against this practice for the purpose of clean code).
James Truher (@JamesWTruher) Is this a future design proposal because I tried it and it did not work. If so, then yes, I would upvote this. As a simple starter I would suggest something that can just suppress a warning on the next executing line similar to ReSharper
#PSSCRIPTANALYZER SuppressOnce PSAvoidUsingCmdletAliases
gci # gets suppressed
iex 'format c' # does not get suppressed
Reacted by Anatoly, ExE Boss, Richard Shade, Frode Flaten, Kamil Kosek, derigo, Bertaz, Hassaan Ahmed, Gilbert Sanchez, exu-g and 3 more
It's definitely not present, I was just exploring the usability of the suggestion. I do think it would be generally useful and something that would benefit from some sort of RFC. I would like to spark the discussion, for sure.
Reacted by Christoph Bergmeister, Jake Gauthier and Hassaan Ahmed
I just posted an issue where this works for suppressing the error from a single variable (PSUseDeclaredVarsMoreThanAssignments), but it doesn't seem to honor the scope flag (unless I am doing something wrong).
I'd like to upvote this - I'd like to see a solution that lets me suppress any of the checks for a section of the code in a file, where a section could be a single line, a couple of lines, or the whole file (I have a couple of ps1 files that purely define variables for our environment, so I get hundreds of variable assigned but never used messages.
changed the title [-]is there a way to mark specific lines to ignore for specific rules?[/-][+]Feature request: Mark specific lines or blocks to ignore for specific rules[/+]on Nov 21, 2018
@swngdnz An RFC might help to push it further and alert about its importance. The only downside is that we could end up creating the most beautiful design that is very hard to implement. We should rather strive to go for a minimum viable improvement to suppress e.g. only the next line/statement. So, yes to being loud, I only fear that RFCs sometimes lead to something being discussed to death. At the end of the day we also need a commitment from someone to implement the proposal, otherwise the RFC is pointless.
Wirth taught me 35 years ago that "best" was the death of progress. That's why he kept designing new languages. :-)
That's why I suggested what I thought would be simple (#pragma once/disable/restore). It's similar to what I did in a C compiler I wrote in the 1980's (although I think I used once/push/pop since it was a stack based compiler).
I'll take a look at it. But I'm not sure my C# skills are good enough (I'm guessing it's C#, if it's javascript, I'm completely out of it).
it's C#. Familiarity with the AST (abstract syntax tree) of PowerShell or at least its tokens are essential but I am happy to help with some bits or gluing stuff together at the end.
Late to the party, but I like it too, even though I have no idea if it would ever be worth the effort for you folks to code it.
BTW, I suppress the specific PSUseDeclaredVarsMoreThanAssignments by assigning variables like this to $null. (I have a few legitimate cases where this is intentional, but don't want to suppress the rule since it is frequently a misspelling or logic error.
I'm running into something similar as tats-u. I am getting warnings for Get-CimInstance and Get-WmiObject and would like to suppress them given that I have properly limited the code execution so that they are never run in an incompatible environment.
function Get-OperatingSystemVersion
{
if (Test-OperatingSystemIsWindows)
{
if ((Get-PSMajorVersion) -ge 3)
{
# Use Get-CimInstance
$arrCimInstances = @(Get-CimInstance "Win32_OperatingSystem" -ErrorAction SilentlyContinue)
} `
else `
{
# Use Get-WmiObject
$arrCimInstances = @(Get-WmiObject "Win32_OperatingSystem" -ErrorAction SilentlyContinue)
}
# ....
}
I would like to be able to use a comment to block certain rules on certain lines/blocks of code. I would greatly prefer not to use the param block for this functionality, as not all my code uses a param block. Also the param block would block the rule for the whole function/code scope, which is not desirable.
Frank Lesniak (@franklesniak) The rule says that starting from PowerShell v3, the CIM cmdlets should be preferred, so not sure why you cannot use them.
If for whatever reason you want to keep using WMI, you could suppress as follows (see here):
functionGet-OperatingSystemVersion
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWMICmdlet','', Justification ='Required for PS 3')]
Param()
if (Test-OperatingSystemIsWindows)
{
if ((Get-PSMajorVersion) -ge3)
{
# Use Get-CimInstance$arrCimInstances=@(Get-CimInstance"Win32_OperatingSystem"-ErrorAction SilentlyContinue)
} `else`
{
# Use Get-WmiObject$arrCimInstances=@(Get-WmiObject"Win32_OperatingSystem"-ErrorAction SilentlyContinue)
}
# ....
}
}
I'd like to add my vote to this issue. There are definitely use cases where a variable will trigger the "assigned but never used" problem when that's not the case.
I would suggest using a similar functionality to that of DevSkim, which allows for a small comment on the line throwing the error to function as a switch to stop the error. adding '#DevSkim: ignore DS104456' to the end of a line that triggers a warning for using a restricted cmdlet, makes the scanner ignore that specific instance of the violation.
Reacted by david-gonzalez-pnw, Brian Scholer, Coby Caldwell, Frode Flaten, Sebastien Gissinger, Frank Lesniak, Bertaz, Codfish, MaBu, Hassaan Ahmed and 1 more
The current situation is indeed ugly. In order to suppress the relevant warning for a few lines - and only a few lines - of a script, I have to do something with this form:
We have a tool that runs PSScriptAnalyzer as part of our Pull Request process. It will not let anything be checked in that has any Errors or Warnings.
Recently, we started seeing a lot of PSUseSingularNouns. In some cases this is intentional. For example: Send-MessageToTeams(As in Microsoft Teams)
We have had to disable that rule so we can keep things the way we want them, but it would be FAR better to be able to disable the rule just in the approved cases.
Reacted by Frank Lesniak, Herb Martin, Tom--, GrabCAD-Gary, Doodad/Doodead, minig0d, Hoàng, Felipe C, bravo-kernel, edgetools and 24 more
Making a note here in case someone else runs into this issue. I noticed that you cannot place the SuppressMessageAttribute on specific parameters in a Param block, but instead you have to place the attribute on the Param block as a whole.
Just ran into this when installing Chocolatey on a clean Windows device for the first time. The Choco docs suggest the use of Invoke-Expression. I'd have liked to suppress the PSAvoidUsingInvokeExpression warning with a comment, but it doesn't seem possible.
Reacted by Frank Lesniak, Sam Darwin, Neodon and James Lamb
Comments should be the way to go IMHO, you do not want to interfere with the script logic itself. eslint also uses the same approach as Gilbert Sanchez (@HeyItsGilbert) mentioned. It also has an disable-next-line comment. Which comes in very handy, so you almost never have to use block with enable and disable, just sometimes.
Reacted by Frank Lesniak, Gilbert Sanchez, Chawye Hsu, Codfish, MaBu, kk738, Ajay Mehta, sp00n, Hassaan Ahmed, Thomas Meckel and 2 more
As a previous commenter wrote: "Just ran into this when installing Chocolatey on a clean Windows device for the first time."
This github issue seems like one of many examples where linux-based tools do things in a simple, obvious, easy way (at least to me) while windows or powershell are convoluted and confusing. On all linters that I have seen before, you can "mark specific lines or blocks to ignore for specific rules."
Reacted by Ajay Mehta, Hassaan Ahmed, Tatsunori Uchino, Thomas Meckel, exu-g and Neodon
I appreciate the ongoing discussion for this issue. I wanted to add my experience since the last update was over a year ago. I just spent a non-trivial amount of time trying to figure out how to suppress a rule one time with a comment.
Comment-based directives for linting are widely supported across languages and tools. I'm sure it's not something that's easy to just implement, however, so please don't take this comment as criticism of the timeline.
Here are some examples for what I mean by widely supported:
Activity
bergmeister commentedon Jan 21, 2018
Andrew Stanton (@StingyJack) You could define a local function and suppress it on this local one only (you can define even define functions inside functions to limit its scope).
Because of the way scoping works in PowerShell (a child scope inherits the variables from its parent), you do not even need to pass in the variables in theory (although personally I am against this practice for the purpose of clean code).
JamesWTruher commentedon Jan 22, 2018
something like?
or something more?
bergmeister commentedon Jan 22, 2018
James Truher (@JamesWTruher) Is this a future design proposal because I tried it and it did not work. If so, then yes, I would upvote this. As a simple starter I would suggest something that can just suppress a warning on the next executing line similar to ReSharper
JamesWTruher commentedon Jan 22, 2018
It's definitely not present, I was just exploring the usability of the suggestion. I do think it would be generally useful and something that would benefit from some sort of RFC. I would like to spark the discussion, for sure.
StingyJack commentedon Jan 23, 2018
James Truher (@JamesWTruher) yes, something like that
gmitch64 commentedon Feb 6, 2018
You should be able to do it with something like
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingCmdletAliases", "")]
I just posted an issue where this works for suppressing the error from a single variable (PSUseDeclaredVarsMoreThanAssignments), but it doesn't seem to honor the scope flag (unless I am doing something wrong).
Graham
gmitch64 commentedon Feb 12, 2018
I'd like to upvote this - I'd like to see a solution that lets me suppress any of the checks for a section of the code in a file, where a section could be a single line, a couple of lines, or the whole file (I have a couple of ps1 files that purely define variables for our environment, so I get hundreds of variable assigned but never used messages.
Graham
[-]is there a way to mark specific lines to ignore for specific rules?[/-][+]Feature request: Mark specific lines or blocks to ignore for specific rules[/+]essentialexch commentedon Nov 21, 2018
Does this need an RFC as James Truher (@JamesWTruher) sort-of suggested?
bergmeister commentedon Nov 21, 2018
@swngdnz An RFC might help to push it further and alert about its importance. The only downside is that we could end up creating the most beautiful design that is very hard to implement. We should rather strive to go for a minimum viable improvement to suppress e.g. only the next line/statement. So, yes to being loud, I only fear that RFCs sometimes lead to something being discussed to death. At the end of the day we also need a commitment from someone to implement the proposal, otherwise the RFC is pointless.
essentialexch commentedon Nov 21, 2018
Wirth taught me 35 years ago that "best" was the death of progress. That's why he kept designing new languages. :-)
That's why I suggested what I thought would be simple (#pragma once/disable/restore). It's similar to what I did in a C compiler I wrote in the 1980's (although I think I used once/push/pop since it was a stack based compiler).
I'll take a look at it. But I'm not sure my C# skills are good enough (I'm guessing it's C#, if it's javascript, I'm completely out of it).
bergmeister commentedon Nov 21, 2018
it's
C#. Familiarity with theAST(abstract syntax tree) of PowerShell or at least its tokens are essential but I am happy to help with some bits or gluing stuff together at the end.HerbM commentedon Oct 6, 2019
Late to the party, but I like it too, even though I have no idea if it would ever be worth the effort for you folks to code it.
BTW, I suppress the specific PSUseDeclaredVarsMoreThanAssignments by assigning variables like this to $null. (I have a few legitimate cases where this is intentional, but don't want to suppress the rule since it is frequently a misspelling or logic error.
tats-u commentedon Dec 23, 2019
I don't want it to vomit an invalid lint to the following code to keep the compatibility between 5- and 6:
franklesniak commentedon Jan 6, 2020
I'm running into something similar as tats-u. I am getting warnings for Get-CimInstance and Get-WmiObject and would like to suppress them given that I have properly limited the code execution so that they are never run in an incompatible environment.
I would like to be able to use a comment to block certain rules on certain lines/blocks of code. I would greatly prefer not to use the param block for this functionality, as not all my code uses a param block. Also the param block would block the rule for the whole function/code scope, which is not desirable.
bergmeister commentedon Jan 6, 2020
Frank Lesniak (@franklesniak) The rule says that starting from PowerShell v3, the CIM cmdlets should be preferred, so not sure why you cannot use them.
If for whatever reason you want to keep using WMI, you could suppress as follows (see here):
Royalty087 commentedon Apr 9, 2020
I'd like to add my vote to this issue. There are definitely use cases where a variable will trigger the "assigned but never used" problem when that's not the case.
I would suggest using a similar functionality to that of DevSkim, which allows for a small comment on the line throwing the error to function as a switch to stop the error. adding '#DevSkim: ignore DS104456' to the end of a line that triggers a warning for using a restricted cmdlet, makes the scanner ignore that specific instance of the violation.
LinuxOnTheDesktop commentedon May 27, 2021
The current situation is indeed ugly. In order to suppress the relevant warning for a few lines - and only a few lines - of a script, I have to do something with this form:
revolter commentedon May 30, 2021
I tried using
but it doesn't work.
VWACRansom commentedon Sep 30, 2021
UPVOTE!!!
We have a tool that runs PSScriptAnalyzer as part of our Pull Request process. It will not let anything be checked in that has any Errors or Warnings.
Recently, we started seeing a lot of PSUseSingularNouns. In some cases this is intentional. For example:
Send-MessageToTeams(As in Microsoft Teams)We have had to disable that rule so we can keep things the way we want them, but it would be FAR better to be able to disable the rule just in the approved cases.
srbrills commentedon Nov 7, 2023
Making a note here in case someone else runs into this issue. I noticed that you cannot place the SuppressMessageAttribute on specific parameters in a Param block, but instead you have to place the attribute on the Param block as a whole.
syedhassaanahmed commentedon Feb 15, 2024
Just ran into this when installing Chocolatey on a clean Windows device for the first time. The Choco docs suggest the use of
Invoke-Expression. I'd have liked to suppress thePSAvoidUsingInvokeExpressionwarning with a comment, but it doesn't seem possible.HeyItsGilbert commentedon Apr 11, 2024
Using comments would be nice. Rubocop does something like:
It also supports having multiple rules disabled with a simple string array.
air2 commentedon Apr 25, 2024
Comments should be the way to go IMHO, you do not want to interfere with the script logic itself. eslint also uses the same approach as Gilbert Sanchez (@HeyItsGilbert) mentioned. It also has an disable-next-line comment. Which comes in very handy, so you almost never have to use block with enable and disable, just sometimes.
sdarwin commentedon Aug 23, 2024
As a previous commenter wrote: "Just ran into this when installing Chocolatey on a clean Windows device for the first time."
This github issue seems like one of many examples where linux-based tools do things in a simple, obvious, easy way (at least to me) while windows or powershell are convoluted and confusing. On all linters that I have seen before, you can "mark specific lines or blocks to ignore for specific rules."
neodon commentedon Oct 14, 2025
I appreciate the ongoing discussion for this issue. I wanted to add my experience since the last update was over a year ago. I just spent a non-trivial amount of time trying to figure out how to suppress a rule one time with a comment.
Comment-based directives for linting are widely supported across languages and tools. I'm sure it's not something that's easy to just implement, however, so please don't take this comment as criticism of the timeline.
Here are some examples for what I mean by widely supported:
// eslint-disable-next-line no-console# pylint: disable=unused-variable# noqa: E501//lint:ignore ST1006 using unconventional receiver name#[allow(clippy::unwrap_used)]#pragma warning disable IDE0051@SuppressWarnings("checkstyle:MagicNumber")#pragma GCC diagnostic ignored "-Wunused-variable"# rubocop:disable Metrics/AbcSize# shellcheck disable=SC2086# noqa: git-latest partial-become# tflint-ignore: aws_instance_invalid_typeApologies if any of those examples are flawed. I had an AI assist me in creating the initial list and I checked/revised it best I could.
Thanks for keeping this discussion going. I wish I had the expertise to help out.