Scott Hanselman

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

September 24, '17 Comments [30] 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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Sunday, 24 September 2017 06:48:08 UTC
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.


Bjarke L
Sunday, 24 September 2017 10:06:35 UTC
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!
Sunday, 24 September 2017 10:11:28 UTC
If I don't have that $env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 file, should I create it?
Kolja Lampe
Sunday, 24 September 2017 10:20:51 UTC
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.
Kolja Lampe
Sunday, 24 September 2017 11:54:59 UTC
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...
Sunday, 24 September 2017 12:03:48 UTC
Or just use
Install-Module z -AddToProfile
from a non administrator command line.
Sunday, 24 September 2017 12:42:33 UTC
https://www.hanselman.com/blog/JumpLocationAChangeDirectoryCDPowerShellCommandThatReadsYourMind.aspx
kyle
Sunday, 24 September 2017 14:52:20 UTC
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.
Sunday, 24 September 2017 17:51:19 UTC
I use Jump-Location for basically the same thing, except with the
j
command
Monday, 25 September 2017 03:04:09 UTC
Very informative writing on this topic . You may know JSC Result 2017 is published by Education Board Results of Bangladesh. To know your JSC Result 2017, Dakhil Result 2017, JSC Result 2017 bd, JSC Result 2017 all board, JSC exam result 2017, JSC exam result 2017 bd, JSC Result Dhaka board, JDC Result 2017 Click on the official site.

JSC Result 2017
Monday, 25 September 2017 07:51:31 UTC
I use Jump Location, which I first learned about from another of your posts:

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

:-)
Adam H
Monday, 25 September 2017 09:37:17 UTC
Oh this is awesome - I love it. Thanks very much, I'll be using this all the time now.

I also learned about the ability to CD into the registry with PowerShell - something so simple that I didn't know about until now!
Monday, 25 September 2017 10:29:22 UTC
PSReadine (in the box in PowerShell 5+ but installable for previous versions) supports Ctrl+R that serves me pretty well.
Monday, 25 September 2017 10:29:22 UTC
PSReadine (in the box in PowerShell 5+ but installable for previous versions) supports Ctrl+R that serves me pretty well.
Monday, 25 September 2017 11:38:50 UTC
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.
Claudio
Monday, 25 September 2017 12:57:15 UTC
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
Monday, 25 September 2017 14:14:01 UTC
Amazing!!! I like this website so much it's really awesome. I have also gone through your other posts too and they are also very much appreciates able and I'm just waiting for your next update to come as I like all your posts... This is so interesting; I have never seen this before, Nice Post. Thank you so much for share.
NU Honours 3rd Year Result 2017
JSC Result 2017
Liton
Monday, 25 September 2017 16:19:28 UTC
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
Monday, 25 September 2017 16:41:40 UTC
Download Gesturereen to use the unique lock screen with signature.
Monday, 25 September 2017 19:44:08 UTC
Something like

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


appears to work if you don't want to use an admin prompt.
Jon
Tuesday, 26 September 2017 12:25:22 UTC
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
Tuesday, 26 September 2017 14:00:53 UTC
good post thanks admin very use ful
Tuesday, 26 September 2017 14:02:25 UTC
mobdro is the one of the best online tv application Bigo Live pc
Download mobdro
use this app thanks admin
Tuesday, 26 September 2017 14:47:03 UTC
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>


Tank
Wednesday, 27 September 2017 17:24:57 UTC
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
Christoph Bergmeister
Wednesday, 27 September 2017 17:47:49 UTC
I have also gone through your other posts too and they are also very much appreciates able and I'm just waiting for your next update to come as I like all your posts... This is so interesting; I have never seen this before, Nice Post. Thank you so much for share. her
Markus Vermund
Friday, 29 September 2017 11:11:35 UTC
absulutly sure you will get more data here click on this link مانتو اداری
Friday, 29 September 2017 11:15:58 UTC
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.

Eddie Butt
Friday, 06 October 2017 04:52:31 UTC
Good post about PowerShell thanks admin very useful.
Hp 15-BE015TU price
Jacowski
Saturday, 07 October 2017 09:41:10 UTC
Actually i need a help in developing websites. First of all can anyone tell me what themes are used in these websites they look best to me
> Custom Printed Boxes
> Pakistani Khanay

And where can i learn to use wordpress fulently.
thankyou!!!!!
christine hanwell
Comments are closed.

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