View Single Post
Old 01-31-2007   #3 (permalink)
Vasily Gusev


 
 

Re: Dynamic variables creation

Better is to use New-Variable:
for ($i=0; $i -le 10; $i++) { New-Variable -Name "dynamicVar_$i" -value $i }
I also fixed the for condition - in your variant code block not called even
once.
And you can simplify it more:
0..10|%{ New-Variable "dynamicVar_$_" $_ }

--
Gusev Vasily, AKA Xaegr
MCP, MCSA
http://xaegr.livejournal.com

"Andrew Savinykh" <andrewsav@gmail.com> wrote in message
news:ua537qRRHHA.1200@TK2MSFTNGP02.phx.gbl...
> Why don't you try something like for ($i=0; $i -le 10; $i++) {
> Invoke-Expression "`$dynamicVar_$i = $i" }
> A. Petitjean wrote:
>> Hi Powerscripters !
>>
>> Here's someone knows how to create dynamic variables with Powershell ?
>>
>> Example : for ($i=0; $i -eq 10; $i++) { $dynamicVar_$i = $i }
>>
>> I want to obtain the creation of $dynamicVar_1, $dynamicVar_2, ...
>> $dynamicVar_10.
>>
>> Best regards,
>>
>> Arnaud Petitjean
>> ---
>> http://www.powershell-scripting.com
>> The french Powershell community


My System SpecsSystem Spec