![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Dynamic variables creation 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 Specs![]() |
| | #2 (permalink) |
| | Re: Dynamic variables creation 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | Re: Dynamic variables creation Vasily Gusev wrote: > I also fixed the for condition - in your variant code block not called > even once. Yep, I also spotted this. > Better is to use New-Variable: > for ($i=0; $i -le 10; $i++) { New-Variable -Name "dynamicVar_$i" -value > $i } > And you can simplify it more: > 0..10|%{ New-Variable "dynamicVar_$_" $_ } Either this, or Set-Variable, if it is required that an existing variable doesn't yeild a error condition. Right? |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Dynamic variables creation Thanks a lot, it's exactly what I wanted to do ! Best regards, Arnaud Petitjean "Vasily Gusev" wrote: > 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| math with "GB/MB/KB" in variables fails, without variables works? | PowerShell | |||
| $Variables | PowerShell | |||
| not using variables | PowerShell | |||