Equivalent of "cd %programfiles%" in PowerShell?

In traditional cmd, we can use cd %programfiles% to switch directory, usually C:\Program Files.

In PowerShell, how can we go to a directory by a environment variable?

Answers


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:

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

Need Your Help

Can I iterate referenced assemblies at design-time in C#?

c# components design-time

I am attempting to write a .NET component. The component will be dropped onto a form/user control and needs to access attributes in assemblies referenced by the components parent form/user control at