Scott Hanselman

Spend less time CD'ing around directories with the PowerShell Z shortcut

September 24, 2017 Comment on this post [18] Posted in PowerShell
Sponsored By

Everyone has a trick for moving around their computer faster. It might be a favorite shell, a series of aliases or shortcuts. I like using popd and pushd to quickly go deep into a directory structure and return exactly where I was.

Another fantastic utility is simply called "Z." There is a shell script for Z at https://github.com/rupa/z that's for *nix, and there's a PowerShell Z command (a fork of the original) at https://github.com/vincpa/z.

As you move around your machine at the command line, Z is adding the directories you usually visit to a file, then using that file to give you instant autocomplete so you can get back there FAST.

If you have Windows 10, you can install Z in seconds like this:

C:\> Install-Module z -AllowClobber

Then just add "Import-Module z" to the end of your Profile, usually at $env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Even better, Z works with pushd, cd, or just "z c:\users\scott" if you like. All those directory changes and moves will be recorded it the Z datafile that is stored in ~\.cdHistory.

What do you think? Do you have a favorite way to move around your file system at the command line?


Sponsor: Get the latest JetBrains Rider preview for .NET Core 2.0 support, Value Tracking and Call Tracking, MSTest runner, new code inspections and refactorings, and the Parallel Stacks view in debugger.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
September 24, 2017 10:48
So this inspired me to publish my: Set-LocationEx.ps1.

It keeps the directories in a session variable - and implements the
cd -


Which will change to the previous directory.

and the
cd


Which will cd to your $home directory.


September 24, 2017 14:06
Promptly installed.

I once built myself something similar, where I had to maintain the list of popular destinations myself.

This is much better and it's nice to just be able to import and use it!
September 24, 2017 14:11
If I don't have that $env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 file, should I create it?
September 24, 2017 14:20
Just creating the file worked.
But I needed to change the command to "Install-Module -Name z -Scope CurrentUser" as I don't want to use elevated rights every time I start powershell.

And it makes it quiet slow to start the shell.
September 24, 2017 15:54
I use Go-Shell for basically the same thing, though it is a manual process to set up the shortcuts. But it does allow me to type gd documents and it will put me in my "documents" folder. This module does sound interesting though...
September 24, 2017 16:03
Or just use
Install-Module z -AddToProfile
from a non administrator command line.
September 24, 2017 18:52
I use the PowerShell Community Extensions, which provide the cd - and cd + commands to go to backwards/forwards in location history.

I also have variables in my profile for my favorite directories, so I can do things like cd $GithubDir, with tab completion for variable names.
September 24, 2017 21:51
I use Jump-Location for basically the same thing, except with the
j
command
September 25, 2017 11:51
I use Jump Location, which I first learned about from another of your posts:

https://www.hanselman.com/blog/JumpLocationAChangeDirectoryCDPowerShellCommandThatReadsYourMind.aspx

:-)
September 25, 2017 14:29
PSReadine (in the box in PowerShell 5+ but installable for previous versions) supports Ctrl+R that serves me pretty well.
September 25, 2017 15:38
Awesome tip! As for what I use, I have several small SlickRun shortcuts with my most common directories. The only issue with this method is that I get a new shell every time, not to change in place.
September 25, 2017 16:57
Nice!

This would be a great addition for baked-in behavior in both the command line and Windows Explorer. Most of the time I use Explorer, and its recent places list has always been very flaky and unreliable. It would a lot nicer if you could type in the address bar and have it give MRU suggestions like this.
Sam
September 25, 2017 20:19
I use a little library I wrote called "markjump".
I mark a favorite location with "m secretproject" and then I can forever jump to it with "j secretproject" or "j se".

It lives here: https://github.com/secretGeek/markjump/blob/master/README.md
September 25, 2017 23:44
Something like

Install-Module -Name "z" -Scope "CurrentUser" 


appears to work if you don't want to use an admin prompt.
Jon
September 26, 2017 16:25
Here's a one-liner that works

Install-Module -Name z -Scope CurrentUser; echo "Import-Module z" > $env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
September 26, 2017 18:47
Great tip indeed, thanks for that. As I mostly use the command line for git, I use a tool called RepoZ to jump from repository to repository directly by just using the git repository name like: grr cd RepoName


C:\> grr cd RepoZ
D:\Develop\GitHub\RepoZ>


September 27, 2017 21:24
I have tried ZLocation a few months ago but found it does not work well together with posh-git. Therefore I am using the slightly inferior version of it called Jump-Location: https://github.com/tkellogg/Jump-Location
September 29, 2017 15:15
Scott, how do you (i.e. tools for recording and converting) make the shell window Animated GIFs in your posts? They really shine in some scenarios versus typing and formatting the code in HTML.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.