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?
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:
cd $env:ProgramFiles"\Common Files"
without + to concatenate the two strings. Can you explain?
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
$e
-tab would autocomplete to $env:
instead of jumping to the first environment variable, though...
Feb 11, 2016 at 16:19