Return value of environment variable with powershell

后端 未结 2 1585
遇见更好的自我
遇见更好的自我 2021-01-20 05:03

I have the name of an environment variable in a variable and I want to get the value. How do I do that? I\'ve tried:

PS C:\\Users\\Joe> $v=\"USERDOMAIN\"
         


        
相关标签:
2条回答
  • 2021-01-20 05:52

    Two lines

    $v = "Path"
    (get-item env:$v).Value
    

    One line

    iex ('$env:' + $x)
    
    0 讨论(0)
  • 2021-01-20 05:54

    To complement Vladimir's helpful answer with a solution that makes direct use of the .NET framework:

    $v="USERDOMAIN"
    [Environment]::GetEnvironmentVariable($v) 
    
    0 讨论(0)
提交回复
热议问题