PowerShellPracticeAndStyle
PowerShellPracticeAndStyle copied to clipboard
Add Set-Variable vs. $foo = "bar" best practice
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? :)__
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 thanks for you answer. :)