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?
Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the OS you can use the below command. You can also use dir env: command to retrieve all environment variables and values.
The cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With