Reputation:
In traditional cmd, we can use cd %programfiles%
to switch directory which usually resolves to C:\Program Files
.
In PowerShell, how can we go to a directory by a environment variable?
Upvotes: 61
Views: 87012
Reputation: 1463
The principle is:
$Env:variablename
So you might try:
cd $Env:Programfiles
or to temporarily switch working directory to %Programfiles%\MyApp
:
Push-Location -Path "$Env:Programfiles\MyApp"
#
# command execution here
#
Pop-Location
To list all environment variables you could do:
Get-ChildItem Env:
or use the convenient alias:
ls env:
Upvotes: 108
Reputation: 16909
To see all the environment variables, do this:
dir env:
To see all the ones containing "Program", do this:
dir env: | ? { $_.Value -match 'Program' }
In PowerShell 3 it is cleaner:
dir env: | ? Value -match 'Program'
The one we want is env:ProgramFiles
, and we can just do this:
cd $env:ProgramFiles
Upvotes: 14