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

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.

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

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.

Spend less time CD'ing around directories with the PowerShell Z shortcut (2) Spend less time CD'ing around directories with the PowerShell Z shortcut (3)
About Newsletter

Hosting By
Spend less time CD'ing around directories with the PowerShell Z shortcut (5)

Comment on this post [18]

Share on or or use the Permalink

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.

Spend less time CD'ing around directories with the PowerShell Z shortcut (6) Bjarke L

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?

Spend less time CD'ing around directories with the PowerShell Z shortcut (8) Kolja Lampe

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.

Spend less time CD'ing around directories with the PowerShell Z shortcut (9) Kolja Lampe

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

:-)

Spend less time CD'ing around directories with the PowerShell Z shortcut (14) Adam H

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.

Spend less time CD'ing around directories with the PowerShell Z shortcut (16) Claudio

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.

Spend less time CD'ing around directories with the PowerShell Z shortcut (17) 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.

Spend less time CD'ing around directories with the PowerShell Z shortcut (19) 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>

Spend less time CD'ing around directories with the PowerShell Z shortcut (21) Tank

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

Spend less time CD'ing around directories with the PowerShell Z shortcut (22) Christoph Bergmeister

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.

Spend less time CD'ing around directories with the PowerShell Z shortcut (23) Eddie Butt

Comments are closed.

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

FAQs

How to use CD command in PowerShell? ›

Therefore, to quickly move the PowerShell to a a specific folder, do the following:
  1. Type cd followed by a space.
  2. Drag and drop the folder on to the PowerShell.
  3. Hit Return.

What is the alternative to cd in PowerShell? ›

When writing PowerShell scripts, it is often necessary to ensure that the script's working directory is set to the location where the script is executed. This can be achieved using the Set-Location cmdlet, which is an alternative to the cd command.

What does the CD command by itself do in PowerShell? ›

cd by itself or cd ~ will always put the user in their home directory. cd . will leave the user in the same directory they are currently in (i.e. the current directory won't change).

How to set directory in PowerShell? ›

You can use the Push-Location and Set-Location commands to change the location to any available drive. For example, if you have a local CD-ROM drive with drive letter D that contains a data CD, you can change the location to the CD drive by entering the Set-Location D: command.

How do I use cd command? ›

The cd command allows you to move between directories. The cd command takes an argument, usually the name of the folder you want to move to, so the full command is cd your-directory . Now that we moved to your Desktop, you can type ls again, then cd into it. We have just changed into a new directory.

What is the cd command in shell script? ›

In the shell, the command cd - is a special case that changes the current working directory to the previous working directory by exchanging the values of the variables PWD and OLDPWD. Note: Repeating this command toggles the current working directory between the current and the previous working directory.

What can I use instead of a cd? ›

A certificate of deposit has a lot of advantages for saving money, but it's not your only option. Several alternatives exist to short term CDs and long term CDs, such as a traditional savings account, a money market account or a club account.

What is the replacement of cd command? ›

Cd Deluxe is a drop-in replacement for the standard cd ("change directory") command. It supports easier access to the history of directories visited.

What is equivalent to cd in CMD? ›

In windows we type dir . In UNIX based systems to change directories we type cd. Convinently, it is the same for windows or alternatively you can use chdir in the command prompt. In addition, in windows, cd , tells us the current directory which is helpful if we cannot tell where we are in the directory structure.

Why use cd command? ›

Use the cd command to move from your present directory to another directory. You must have execute (search) permission in the specified directory. If you do not specify a Directory parameter, the cd command moves you to your login directory ($HOME in the ksh and bsh environments, or $home in the csh environment).

Does PowerShell do everything CMD does? ›

CMD supports native Windows executables. While some third-party tools can be executed using CMD, the integration is limited by CMD's limitations, such as text-based output, limited API interaction, and basic scripting capabilities. PowerShell supports an extensive list of programs, both Windows-native and third-party.

How do I move files from one directory to another in PowerShell? ›

In the PowerShell prompt, type Get-Item –Path c:\testfolder\*. txt | Move-Item -Destination c:\temp and press ENTER. This command moves all . txt files from the testfolder directory to c:\temp.

What is the CD command in PowerShell? ›

To work with the path in PowerShell, you can use a number of built-in commands and operators. The most commonly used command is "cd", which stands for "change directory". This command allows you to navigate to a different directory by specifying the path to that directory.

What is the difference between cd and set location in PowerShell? ›

The CD command is what is known as an alias. In PowerShell, an alias is essentially just a shortcut. It's a shortened command that takes the place of a longer command. The longer command (which PowerShell calls a cmdlet) for which CD is an alias is Set-Location.

How do I list everything in a directory in PowerShell? ›

You can get all items directly within a folder using Get-ChildItem . Add the optional Force parameter to display hidden or system items. For example, this command displays the direct contents of PowerShell Drive C: .

How do I run a cd from the command-line? ›

Open the Command Prompt (CMD) and type "cd" with a space, followed by the name of the directory, or drag and drop the directory into CMD from File Explorer. Press "Enter." Type "cd.." and press "Enter" to go back one directory. Type "cd\" and press "Enter" to go to the root of the drive.

How to get current directory in PowerShell? ›

Another way to get the current directory in PowerShell is by using the $PWD variable. This variable holds the current working directory as a PowerShell object. To display the current directory using this method, type “$PWD” and press Enter. The full path of the current directory will be displayed.

How to execute a file in PowerShell? ›

Direct execution
  1. Open PowerShell on your PowerShell terminal.
  2. Specify the Path: Navigate to the directory where the file is located. For example, if your .exe file is located in “C:\Program Files\MyApp,” you would use:
  3. cd C:\Program Files\MyApp.
  4. Execute the .exe file: Once you're in the correct directory, you can.
Feb 7, 2024

How to get files from a folder in PowerShell? ›

You can get all items directly within a folder using Get-ChildItem . Add the optional Force parameter to display hidden or system items. For example, this command displays the direct contents of PowerShell Drive C: .

References

Top Articles
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 6083

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.