Dynamically create variables in PowerShell

后端 未结 1 1652
孤独总比滥情好
孤独总比滥情好 2020-11-27 17:48

How do I create a new variable each time a loop runs?

Something along the lines of

for ($i=1; $i -le 5; $i++)
{
    $\"var + $i\" = $i
    write-hos         


        
相关标签:
1条回答
  • 2020-11-27 18:10

    Use New-Variable and Get-Variable (mind available options including scopes). E.g.

    for ($i=1; $i -le 5; $i++)
    {
        New-Variable -Name "var$i" -Value $i
        Get-Variable -Name "var$i" -ValueOnly
    }
    
    0 讨论(0)
提交回复
热议问题