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.

Upvotes: 1

Views: 79

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60968

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

Upvotes: 2

Related Questions