How to quickly change directories in PowerShell

by Doug Finke on August 29, 2011

in Microsoft,PowerShell

I’ve developed a habit for changing directories in PowerShell. For directories I frequent most, I add the following to my $PROFILE. Let’s say the target directory is C:\Windows\System32.

function cdSystem32 { cd "C:\Windows\System32" }

This has a few advantages. When I am at the command prompt, I can type cds press <Tab> then <ENTER> and I am done. I don’t have to type it all out, I don’t have to remember the full name and if I have more than one function with similar beginning characters (like, cdSys, cdSystem) I can cycle through them by pressing <Tab> till I get to the right one.

This is super helpful when I am working on PowerShell modules overtime and when I work on teams with other C# developers. If there are many solutions for the  project, it is sometimes organized across several directories.

Companion Function

I have a companion function gencd. So when I am in a directory that I know I will frequent, I type gencd and copy and paste the result to my $PROFILE. Gencd generates the PowerShell line at the beginning of this post.

function gencd {            
@"
function cd$(split-path $pwd -leaf) { cd "$pwd" }
"@            
}

I haven’t automated it to the point of adding it to the end of my $PROFILE. I am not sure if it is worth it.

{ 7 comments… read them below or add one }

xcud 08.29.11 at 9:17 pm

A couple of Set-Location nuggets from my profile:

function GoUp($path) { Set-Location -path “..\$path” }; Set-Alias .. GoUp;Set-Alias cd.. GoUp
function GoUpUp($path) { Set-Location -path “..\..\$path” }; Set-Alias … GoUpUp
function GoHome($path) { Set-Location -path “$home\$path” } Set-Alias home GoHome

.. # goes to parent folder
.. Foo # goes to sibling folder named ‘Foo’

Jeffery Hicks 08.29.11 at 9:59 pm

What happens if the path has a space? The function name won’t work. You might consider stripping out any spaces. I think it would be nice that when you gen the function you automatically add it to your session at least to save the hassle of copy and paste. As for the profile, you could always prompt.

Doug Finke 08.30.11 at 7:34 am

Thanks for the nuggets Ben.

Thanks Jeffery, good points. I will update an re-post. Interesting, I have not worked with a directory with a space in it on the projects I have done and used this approach.

I need to get out more :)

Doug

Oisin G. 08.31.11 at 7:52 pm

I’ve been pondering an NCD (norton change directory) module since forever. We should collaborate on one.

Will Steele 09.01.11 at 9:07 am

I like this idea and added the up functions to my module. Here is a Down function. It works with $PWD by default, but, this can be overridden. Thanks for the idea.

function Windows.Switch-ToChildFolder {

param(
$Path = $PWD
)

pushd $pwd
$dir = gci $path | Where {$_.PSIsContainer} | select fullname;
if($dir.length -gt 0) {
Write-Output “There are $($dir.length) children folders.”;
for($i = 1; $i -lt $dir.length; $i++) {
Write-Output “$($i): $($dir[($i-1)].fullname)”;
}
$response = Read-Host “Type the number for the folder you want to select”;
cd $dir[$($response-1)].fullname;
} else {
Write-Output “The current directory has no children”;
}

} # end function Windows.Switch-ToChildFolder

# Set alias
Set-Alias -Name w.stcf -Value “Windows.Switch-ToChildFolder”
Set-Alias -Name Down -Value “Windows.Switch-ToChildFolder”
Set-Alias -Name d -Value “Windows.Switch-ToChildFolder”

Will Steele 09.01.11 at 9:15 am

Whoops. Off by one in the folder enumeration:

# Define function
function Windows.Switch-ToChildFolder {

param(
$Path = $PWD
)

pushd $pwd
$dir = gci $path | Where {$_.PSIsContainer} | select fullname;
if($dir.length -gt 0) {
Write-Output “There are $($dir.length) children folders.”;
for($i = 1; $i -le $dir.length; $i++) {
Write-Output “$($i): $($dir[($i-1)].fullname)”;
}
$response = Read-Host “Type the number for the folder you want to select”;
cd $dir[$($response-1)].fullname | Out-Null;
} else {
Write-Output “The current directory has no children”;
}

} # end function Windows.Switch-ToChildFolder

# Set alias
Set-Alias -Name w.stcf -Value “Windows.Switch-ToChildFolder”
Set-Alias -Name Down -Value “Windows.Switch-ToChildFolder”
Set-Alias -Name d -Value “Windows.Switch-ToChildFolder”

Doug Finke 09.01.11 at 11:21 am

Woohoo, Oisin pulls out the NCD. I’m on the same page and time frame. The reason I didn’t jump that rabbit hole is I haven’t used NCD in such a long time I don’t remember all the features.

Thanks Will for the excellent additions.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

You are forbidden!