在 PowerShell 中动态创建变量 [英] Dynamically create variables in PowerShell

查看:24
本文介绍了在 PowerShell 中动态创建变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在每次循环运行时创建一个新变量?

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

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

推荐答案

Use New-VariableGet-Variable (注意可用选项,包括范围).例如

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
}

这篇关于在 PowerShell 中动态创建变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆