Search code examples
powershellenvironment-variablesindirection

How to retrieve environment variable by variable in `Powershell`?


I have name of my environment variable in Powershell script:

$credEnvName = "Confluence_Data"

How can i retrieve the environment value by this variable?

I tryied this:

$value = ${env:$credEnvName}

But its not working.


Solution

  • You can use Get-Content to retrieve its value:

    $env:Confluence_Data = 'hello world'
    $credEnvName = 'Confluence_Data'
    Get-Content -Path env:$credEnvName # hello world
    

    Get-Item expanding on the .Value property is another option:

    (Get-Item env:$credEnvName).Value