PowerShell tricks – Using dot(s) to refer to the current location

9663950111_c97678228e_m
Most people are aware that PowerShell supports commandline navigation in the same way as the good old command prompt (see my previous post Improve PowerShell commandline navigation for ways to enhance this):

cd $env:USERPROFILE\Desktop
Resolve-Path '.'
#change to the current direction (doing nothing)
cd .
#move up one level
Resolve-Path '..'
cd ..

The above is using cd as the alias for the Set-Location Cmdlet providing:

  • One dot as an argument for the Path parameter representing the current location (changing the location to the current location does effectively nothing)
  • Two dots for the Path as an argument for the Path parameter representing one level higher than the current location

Since this is implemented by the Provider (at least that’s what I believe) the same concept can be used in many different places, basically every built-in command that has a Path parameter.:

#get all the built-in commands that have a Path parameter
Get-Command | where { $_.Parameters -and $_.Parameters.ContainsKey('Path') -and $_.HelpURI -and $_.HelpURI.StartsWith('http://go.microsoft.com') }

#navigation works also with the registry provider
cd HKLM:\Software\Microsoft
cd ..
cd c:
#open windows explorer using the current location (ii is the alias for Invoke-Item)
ii .
#same one level higher
ii ..
#Within ISE open all files in the current folder or one level higher
psedit .
psedit ..
#copy the current folder and its content to a folder one level higher
copy * ..\test

Please share if you know of more tricks using dot(s) as an argument to the Path parameter.

shareThoughts


Photo Credit: Rein -e- Art via Compfight cc

Advertisements

I'd love to hear what you think

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s