Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Dynamic variables creation

Reply
 
Old 01-31-2007   #1 (permalink)
A. Petitjean


 
 

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 SpecsSystem Spec
Old 01-31-2007   #2 (permalink)
Andrew Savinykh


 
 

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 SpecsSystem Spec
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
Old 01-31-2007   #4 (permalink)
Andrew Savinykh


 
 

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 SpecsSystem Spec
Old 01-31-2007   #5 (permalink)
A. Petitjean


 
 

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 SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
math with "GB/MB/KB" in variables fails, without variables works? PowerShell
$Variables PowerShell
not using variables PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46