PoshCode / PowerShellPracticeAndStyle

The Unofficial PowerShell Best Practices and Style Guide
https://poshcode.gitbooks.io/powershell-practice-and-style
Other
2.24k stars 289 forks source link

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

Open tscholze opened 4 years ago

tscholze commented 4 years ago

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? :)__

rkeithhill commented 4 years ago

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.

tscholze commented 4 years ago

@rkeithhill thanks for you answer. :)