Don’t do that #7: Multiple lines to create variables with the same prefix

By | March 20, 2015

Don’t do that: The following code requires multiple lines to create variables (with the same prefix “number”):

$number1 = 0
$number2 = 0
$number3 = 0
$number4 = 0
$number5 = 0

Do that: It’s possible to create multiple variables in one line and initialize them with a specific value, 0 for example:

1..5 | ForEach-Object -Process {New-Variable -Name "number$_" -Value 0}


Leave a Reply

Your email address will not be published.