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

59 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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