1

I think we can do this Clear-Variable -Name Facility_Name, StudentDOB, StudentLastName

However i have around 200 variables that needs to be cleared after the foreach loop, wanted to know if there is a easy way to clear all these local variables in one shot?

Thanks

4
  • 1
    Please show us your code and whatever you have tried so far Commented Nov 7, 2020 at 22:31
  • 2
    i suspect that you are doing things wrongly when creating so very many variables. for instance, you list values that SHOULD be stored in properties of an object. instead of $StudentDOB one likely otta use $Student.DOB - note the dot delimiting the variable $Student and the property DOB. Commented Nov 7, 2020 at 22:59
  • Yes, for your custom variables. Get in the habit of adding a prefix them with say 2-3 chars. Say, using the first 3 of your name. So, like 'sanSomeVariableName', then clear or remove them using 'san*'. Commented Nov 8, 2020 at 6:33
  • 1
    To compliment, @Lee_Dailey's, I would probably use a hash table: $Student = @{}; $Student['DOB'] = 'my DOB'. Anyways, please show the code in the question, so that we can better help you. Commented Nov 8, 2020 at 8:55

1 Answer 1

1

Note that Lee_Dailey and iRon make sensible recommendations regarding avoiding a large number of variables to begin with:

  • Collect multiple values in arrays rather than in individual variables.

  • Alternatively, for named access, create multiple values as named (keyed) entries of a single hashtable object or as properties of a single [pscustomobject] instance.

To address the question as asked:


Note that Clear-Variable resets the values of variables to $null, it doesn't actually remove the variables themselves.

To remove variables as a whole, use Remove-Variable.

Both cmdlets:

  • accept wildcard patterns as arguments for the -Name parameter; pattern * is the one that matches all names.

    • Important: Do not include $ in the name (pattern); e.g., to remove variable $foo, use Remove-Variable foo.
      (If you mistakenly tried Remove-Variable $foo, the value of variable $foo would be used as the name.)
  • have a -Scope parameter that specifies the scope whose variables to target, namely Global, Script, Local, or a numbered scope where 0 is the same as Local, 1 is the parent scope, 2 is the grandparent scope, and so on.

postanote suggests using a shared name prefix, say foo_, for those variables you want to remove all at once, which then allows you to call something like:

Remove-Variable -Scope Local foo_*

Taking a step back: If there's a block of code with variables that you want to restrict to that block, simply use a script block ({ ... }) and invoke it with &, the call operator, which runs the script block in a child scope whose variables go out of scope when execution of the block ends; e.g.:

# Any variables created inside the { ... } block executed 
# with & are scoped to that block.
& {
  foreach ($i in 1..3) {
    $j = $i
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@not2qubit, indeed. I've updated the answer to point out this pitfall.

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.