PowerShell / PowerShell/PSScriptAnalyzer

Rule request: Warning if `Set`/`Get-Variable` is invoked with only the basic `-Name`/`-Value` parameters

Open
#1,706 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Area - Rules Issue - New Rule Up-for-Grabs
Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

Referring to StackOverflow questions along with: How do I automaticaly create and use variable names?.

The cmdlets:

Could completely mislead a novice PowerShell scripter in a wrong programming direction.

The general learning process of a scripter/programmer is playing with variables. Than it gets extended to list of variables like $Var1 = 'One', $Var2 = 'Two', .... The list extends further and they understand they need to automate this somehow but finding guidance for this is quiet a leap and if they ask for guidance at e.g. StackOverflow they often get a direct answer/comment in the direction of using one of these <verb>-Variable cmdlets which is not a good practice for these users and what they atempt to do or learn.

Afaik, there is no good reason to use one of these <verb>-Variable cmdlets which just -Name and -Value parameters.

e.g. Get-Variable MyVar can be replaced by $MyVar and Set-Variable MyVar 'Test' can be replaced by $MyVar = 'Test'

The only other reason to use just the -Name and -Value parameters is to create dynamic variable names (variable indirection) like:

Set-Variable "Var$i" 'Test'

Which is to my opinion a bad practice as they will be added to the same dictionary as the rest of the parameters and one might easily overwrite other general variables and lose the oversight.

Summary of the new feature

When it comes to creating a dynamic variables, a separated dictionary (in the form of a hashtable) should be created and a warning/hint could be given in that direction.

Visa verse, advanced users that know what they are doing, might simply avoid the warning by adding a specific (e.g. -Scope Script) parameter.

What is the latest version of PSScriptAnalyzer at the point of writing
1.19.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing existing PSScriptAnalyzer rule implementations and tests, then trace how calls to Get-Variable, Set-Variable, and New-Variable are analyzed. Define the warning condition for calls using only basic name/value parameters and the exemption for an explicit scope parameter; done means the rule and its tests cover those cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.