PowerShellPracticeAndStyle icon indicating copy to clipboard operation
PowerShellPracticeAndStyle copied to clipboard

Add Set-Variable vs. $foo = "bar" best practice

Open tscholze opened this issue 5 years ago • 2 comments
trafficstars

Hi,

I'm a beginner and a little bit confused in which cases I should use the following syntax:

Set-Variable -Name "desc" -Value "A description"

and ich which cases it is ok do to the simpler

$desc = "A description"

I could not find any helpful information in the internet, maybe someone has a link for me? :)__

tscholze avatar May 24 '20 19:05 tscholze

In general, use variable assignment (your second case above). Set-Variable is handy if the name of a variable is itself contained in a variable e.g. Set-Variable -Name $varname -Value 42. Set-Variable is also handy if you need to specify Option, Visibility or Scope which is somewhat uncommon compared to variable assignment.

rkeithhill avatar May 24 '20 20:05 rkeithhill

@rkeithhill thanks for you answer. :)

tscholze avatar May 25 '20 06:05 tscholze