In Windows, changing directories is a fundamental part of file management, but would you settle for writing full paths of each directory you visit? Luckily, PowerShell change directory commands can make your life easier.
In this tutorial, you will explore the commands available to change directories in PowerShell, helping you navigate your file system effortlessly.
Read on and master the basics of file system navigation with PowerShell today!
Prerequisites
This tutorial will be a hands-on demonstration. To follow along, ensure you have a Windows system with PowerShell installed. This tutorial uses Windows 10 and PowerShell v7.
Switching from One Drive to Another
Each storage device, such as a hard disk or USB drive, is assigned a drive letter (i.e., C:, D:, E:, …Z:). When you open a PowerShell window, the prompt is in your user profile directory on the current drive by default.
Suppose you need frequent access to files or folders stored on a different drive than the one you are currently on. If so, switching from one drive to another is necessary.
To see how to switch from one drive to another:
1. Open PowerShell and confirm if the prompt says C:\Users\admin. If so, then you are currently on the C: drive in the admin user’s profile directory, as shown below.
2. Next, open another PowerShell session, but this time, as an administrator.
Below, the prompt starts in a different default directory than launching PowerShell as a regular user. Specifically, the prompt starts in the System32 directory within the Windows directory on the current drive (C:), as shown below.
This default behavior prevents non-administrative users from accidentally modifying critical system files or running sensitive tools.
3. Run the below command to change the current drive to D:
(or to any available drive). This syntax is a quick way to navigate between different drives in PowerShell.
When switching drives in PowerShell with this method, the prompt changes to the root directory of the new drive, as in the output below.
As you can see, regardless of the default directory that PowerShell starts in, you always can switch to another drive.
4. Now, run the below command to attempt switching to the D:\Kitty
directory (or to any other subdirectory).
Did you get the same error as below? This error shows since you cannot switch to subdirectories (for example, D:\Kitty) in one go by this method.
Jump to the following section to see how to switch to subdirectories.
Navigating the File System via the cd
Command
Navigating multiple subdirectories is a common task in PowerShell. But is there a quick way to navigate through different subdirectories? Yes! The cd
command, short for “change directory,” lets you quickly switch to another subdirectory regardless of your current directory.
Back in the day, the
cd
command was used to navigate between drives and directories in the Command Prompt. Microsoft made thecd
command backward compatible so you can still navigate between directories in PowerShell.
Read on and see how to navigate your file system with the cd
command.
Moving to a Directory in a Different Drive
Jumping to a subdirectory within a different drive may seem like a hassle if you think about it. Well, there is no need to overthink it. You will be surprised how quickly the cd
command lets you jump to another drive while moving into one of its directories.
Run the below command to navigate to the subdirectory called Kitty
in the D:*
drive.
Moving One to Many Levels Up in the Directory Hierarchy
With file system management, or when working on a project, you may frequently need to move up one or two levels from your current directory. The cd command supports the ..
notation that lets you effectively switch to the parent directory of your current directory.
This feature lets you move up a level in your file system without specifying the full path to the parent directory.
1. Run the cd
command below, append a space and the ..
notation, which tells PowerShell to go up one level in the directory hierarchy.
The space character after the
cd
command is not required, as the command will work fine without it. But adding a space is a common practice for better readability and consistency with other commands.
In the output below, you can see the prompt moves up one level from the C:\Users\admin\Documents to the C:\Users\admin directory. Mind you that you did not have to specify the full path for the C:\Users\admin directory to move up.
2. Instead of moving one level up, run the following command with two ..
notations, appending a backslash (\
) at the end of each. Doing so lets you move up two levels in the directory hierarchy.
Below, the output shows you jumped from the C:\Users\admin\Documents straight to the C:\Users directory.
3. Lastly, run the command below to move to the root directory of your current drive.
If you got lost in your file system, this command comes in handy as you can quickly jump to the root directory. This command saves your the hassle of running the cd ..
command multiple times.
Moving Into a Specific Subdirectory
Like moving a level up in the directory hierarchy, moving into a specific subdirectory is also a quick task. Since there might be many subdirectories, appending notations like the previous ones (..
, and ..\
) will not work, you must specify the subdirectory’s name.
Run the below command to move into one of the subdirectories (\admin
) in your current directory (.
).
Notice below that you saved time and effort since you did not have to specify the full path of the C:\Users\admin directory.
Navigating the File System with PowerShell Cmdlets
Although the cd
command undoubtedly works in PowerShell, Microsoft has introduced many cmdlets, like Set-Location
, that let you navigate through your file system.
These cmdlets provide more robust functionality than the cd
command that you can use to switch between drives, navigate stacked locations, and cycle through directories in the location history.
Stay tuned and witness how powerful PowerShell cmdlets are in navigating your file system.
Setting a Location from a Different Drive
Like the cd
command, the Set-Location
cmdlet allows you to jump from one directory to another and navigate straight to one of its directories. But since you are using a PowerShell cmdlet, you must append the -Path
parameter to set the path of your target location.
The Set-Location
cmdlet sets the current working directory to a specified location. That specified location can be one of the following:
- A directory, or subdirectory on the local file system.
- Any other location supported by a provider in PowerShell.
- Or a registry location.
To see how the Set-Location
cmdlet works in navigating your file system:
Run the below command to navigate to the Kitty
directory in your D:
drive specified in the -Path
parameter.
Set-Location -Path "D:\Kitty"
If you feel a bit lazy typing the full
Set-Location
cmdlet, you can also use one of its aliases, likechdir
andsl
, to navigate your file system. Aliases provide a more convenient way to access frequently used commands.
Now, run the below command to set the working location to a specific registry location. In this case, the HKLM:\
path (a shorthand) refers to the HKEY_LOCAL_MACHINE hive in your Windows Registry.
This command sets the working location to the root of the HKEY_LOCAL_MACHINE hive in the registry. Navigating to a registry allows you to access and modify its subkeys and values via other PowerShell commands.
Set-Location -Path "HKLM:\”
Switching Between Stacked Locations (Push and Pop)
Quickly accessing stacked locations sounds convenient. But is it possible? Yes! The Push-Location
and Pop-Location
cmdlets let you push and pop locations in a last-in-first-out (LIFO) order from a named stack.
To navigate stacked locations, follow these steps:
1. Run the below Push-Location
command to push your current location to a stack called Paths
and set the C:\Program Files\PowerShell\
directory as the current directory.
Push-Location -Path 'C:\Program Files\PowerShell\' -StackName "Paths"
2. Next, run the command below to push your current location (C:\Program Files\PowerShell\) to the top of the same Paths
stack.
Push-Location -Path 'C:\Program Files\WindowsPowerShell\' -StackName "Paths"
3. Run each command below to set Paths
as the default stack and view (Get-Location
) the stacked locations.
# Sets the Paths stack as default
Set-Location -StackName "Paths"
# View all stacked paths in the default stack
Get-Location -Stack
Below, you can see you currently have two locations stacked, where the last location you pushed is at the top.
4. Now, run the following Pop-Location
command twice to pop the top stacked location from the default stack, which currently is the Paths stack.
Note that popping the location from the stack removes the location from the stack. The error below indicates that you have popped all stacked locations from the default stack.
Navigating to a Directory Stored in a Variable
You have gone through different ways to navigate directories in your file system. But what if you need to navigate a specific location frequently? Typing full paths multiple times or cycling through the location history can be tedious. Why not store a directory’s full path in a variable instead?
With a declared variable, you can navigate to your target directory without specifying the path off the top of your head.
1. Run the below commands to store the C:\Program Files\PowerShell\
directory to the $psh
variable, and set that location ($psh
) as the current directory.
# Store a directory path to a variable
$psh = 'C:\Program Files\PowerShell\'
# Set the directory from the $psh variable as the current directory
Set-Location -Path $psh
The output below confirms successful directory change using a variable. Regardless of your current directory, you can quickly navigate to the C:\Program Files\PowerShell\ directory via the $psh
variable.
But note that this method only works for the current PowerShell session unless you persist the variable, as demonstrated in the following step.
2. Next, run the below commands, which do not provide output, but persist the ev_psh
variable as an environment variable (SetEnvironmentVariable
) with the value of the directory’s full path.
# Add psh as an environment variable
[System.Environment]::SetEnvironmentVariable('ev_psh', 'C:\Program Files\PowerShell\', 'Machine')
# Reload the newly-added environment variable (ev_psh)
$Env:ev_psh = [System.Environment]::GetEnvironmentVariable("ev_psh","Machine")
3. Lastly, run the below command to set the value from the ev_psh
environment variable as the current directory. Ensure that you are in a different directory than the one in your ev_psh
variable.
Set-Location -Path $env:ev_psh
Cycling Through Directories in PowerShell’s Location History
Another quick way to cycle through directories in PowerShell is using the location history. The -Path
parameter tells the Set-Location
cmdlet to navigate to another directory from the location history (previous and following), depending on the specified value (-
or +
).
To cycle through directories in PowerShell’s location history:
Run the following commands set locations from the history as the current directory. The +
character takes you forward in location history, while the -
character takes you back.
# Sets the system root directory as the current directory
Set-Location -Path $env:SystemRoot
# Navigates back to the previous directory in history (certificate provider)
Set-Location -Path -
# Navigates back to the previous directory in history again (system root directory)
Set-Location -Path -
# Navigates forward to the following working directory in history (certificate provider)
Set-Location -Path +
# Navigates forward to the following working directory in history (HKEY_LOCAL_MACHINE hive)
Set-Location -Path +
Conclusion
File system navigation can be tricky when your options are limited. But with PowerShell change directory commands, like Set-Location
, you have plenty of options. How you wish to navigate your file system more efficiently is entirely up to you.
In addition, mastering the use of Push-Location
and Pop-Location
to save and recall recent locations is a valuable skill.
These techniques allow you to move quickly and seamlessly through directories and locations in PowerShell. But to further improve, why not consider learning about additional commands, like Get-ChildItem
, which can save you plenty of time in your daily tasks?
To navigate to a specific directory in PowerShell, use the `Set-Location` or `cd` command followed by the path of the directory you want to access.
Set-Location 'C:\Path\To\Your\Directory'
# or simply
cd 'C:\Path\To\Your\Directory'
Understanding Directories in PowerShell
What Are Directories?
Directories are fundamental components of a computer’s file system, acting as containers that store files and subdirectories. Mastering how to navigate these directories is essential for effectively utilizing PowerShell, as it allows users to locate and manage files effortlessly.
The File System in PowerShell
In PowerShell, the file system operates similarly to other command-line interfaces but boasts unique commands and capabilities. Understanding the structure of the file system is crucial; drives are typically represented by letters (e.g., `C:\`, `D:\`), with a hierarchy that includes folders and files.
Common commands that act as building blocks for directory navigation include `Get-ChildItem`, which lists the contents of a directory, and `Set-Location`, the primary command for changing the current directory.
Navigating Your PowerShell Home Directory with Ease
Getting Started with PowerShell Commands
Opening PowerShell
PowerShell can be opened in a variety of ways, enabling users to start working efficiently. Some common methods include:
- Using the Start Menu: Click on the Start Menu and type «PowerShell» to locate the application.
- Using Run Dialog: Press `Win + R` to open the Run dialog, type `powershell`, and hit Enter.
Mastering PowerShell DirectoryInfo for Quick File Management
Basic Commands to Navigate Directories
Set-Location Command
The `Set-Location` command is your primary tool for changing the current working directory in PowerShell. This command allows you to specify a path to which you want to navigate.
Syntax of Set-Location
The syntax for using `Set-Location` is straightforward:
Set-Location -Path <Path>
Navigating to a Specific Directory
Users can navigate to directories by utilizing both absolute and relative paths, which play critical roles in file management.
Using Absolute Paths
An absolute path specifies the complete address of a directory, starting from the root of the file system.
Example:
Set-Location -Path "C:\Users\YourUsername\Documents"
This command instantly takes you to the Documents folder, regardless of your current location in the file system.
Using Relative Paths
Relative paths are used in relation to the current directory. They can significantly enhance efficiency when navigating between neighboring directories.
Example:
Set-Location -Path "..\Desktop"
This command moves you up one level to the parent directory and then into the Desktop folder.
PowerShell Get Directory of Script: A Simple Guide
Alternatives to Set-Location
Using the cd Alias
PowerShell allows the use of aliases for commands, making it easier for users to navigate quickly. The `cd` alias is commonly used for `Set-Location`.
Example:
cd "C:\Users\YourUsername\Downloads"
This command effectively achieves the same outcome as using `Set-Location`.
Combining Navigational Commands
You can also combine commands to facilitate quicker navigation through the directory structure.
Example:
cd ..\.. # Moves up two levels
cd "C:\Program Files" # Then goes to Program Files
By being deliberate with your path combinations, you can streamline your workflow.
Mastering PowerShell: Copy Directory Recursively Made Easy
Tab Completion and Its Benefits
What is Tab Completion?
Tab completion is a powerful feature in PowerShell that helps speed up directory navigation and reduce typographical errors. When navigating to a folder, typing the initial letters of the folder name followed by hitting the Tab key prompts PowerShell to auto-complete the entry.
How to Use Tab Completion Effectively
Using tab completion can aid not only in saving time but also in confirming directory names without needing to type the full name.
Example of Tab Completion:
cd C:\Us[Tab]\
When the user types `C:\Us` and then presses the `Tab` key, PowerShell completes the entry automatically, saving time and reducing the chance of errors.
PowerShell Get Directory From File Path: A Simple Guide
Viewing the Current Directory
Using Get-Location
To check your current directory, the `Get-Location` command is invaluable. It immediately displays the path of the directory you’re currently working in, helping you understand your context.
Example:
Get-Location
This command will return the full path of your current directory, allowing you to confirm you’re where you intend to be.
PowerShell Compare Directories: A Simple Guide
Common Mistakes to Avoid
Typographical Errors
One common pitfall when navigating directories is entering incorrect names, particularly those containing spaces or special characters. Always place paths with spaces in quotation marks to ensure they are correctly recognized by PowerShell.
Wrong Context
Understanding the current directory context is crucial. If you attempt to access a file in a different directory without navigating there first, you may encounter errors.
PowerShell Direct: Your Guide to Simplified Commands
Tips for Efficient Directory Navigation in PowerShell
Creating Shortcuts
Shortcuts can be beneficial for quickly accessing frequently-used directories. You can create symbolic links using the `New-Item` command.
Example:
New-Item -ItemType SymbolicLink -Path "C:\MyShortcut" -Target "C:\Users\YourUsername\Documents"
This command creates a shortcut that directs you to the Documents folder from `C:\MyShortcut`, enhancing efficiency.
Using PowerShell Profile for Custom Aliases
Customizing your PowerShell experience can streamline your workflow. Editing your PowerShell profile allows you to set up permanent aliases for commonly accessed paths.
Steps to Edit PowerShell Profile:
-
Open your profile file:
notepad $PROFILE
-
Add a custom alias:
Set-Alias docs "C:\Users\YourUsername\Documents"
Conclusion
Mastering the ability to navigate directories using PowerShell is essential for improving efficiency and productivity. By utilizing commands like `Set-Location`, `cd`, and effective practices such as tab completion and alias creation, users can significantly enhance their command-line experience. Practice these techniques to build your confidence in navigating the PowerShell environment seamlessly.
In this short video, I’m going to show you some ways to navigate folders, aka directories, with PowerShell.
I’ll start with navigating folders, also known as directories.
I have a two-window setup to better show what’s going on with both PowerShell and File Explorer.
By default, an elevated PowerShell starts in the C:\Windows\System32 folder.
Get-Location
The Get-Location
(alias pwd
) command outputs the folder path that I’m in to the console.
For those that are more familiar with the linux command or command prompt commands, the alias pwd
can be substituted for Get-Location.
Set-Location
To move to another folder, use the Set-Location
command. Type the command followed by the path to set the location to.
The alias cd
can be used as a substitute.
If I wanted to change to another folder, I can navigate to the folder in File Explorer and copy the path from the address bar and paste into PowerShell.
Copy As Path
Another technique using File Explorer is to hold Shift and right-click on the folder and select “Copy as path” to copy the folder path to the clipboard.
This is particularly useful as it includes double quotes which is needed for paths with spaces.
Relative Paths
You can also use relative folder paths. Using ..
with cd
will change the current working directory to the parent directory.
A .\
is used to mean the working directory, so I can type .\Lab to mean the Lab folder in C:\PowerShellBreakdown.
Location Stack
In File Explorer, when you navigate folders, you can use the Back button to go a previous folder.
In PowerShell, something called the “Location Stack” is a way to have something similar to navigating through the browsing history.
Push-Location
Each time you “push” to go to a new directory the current directory is added to the stack to which creates something like a history of working directories which you can go back through, or “pop” to later.
From the current location of C:\PowerShellBreakdown, I will use the command Push-Location
to adds the current location to the top of a location stack and changes the location to what’s specified.
When using Get-Location
, I can add the -Stack
parameter to view the location stack.
Each time I push to another location with Push-Location
or the alias pushd
, I can see that the current location has been added to the top of the stack.
Pop-Location
To change locations to the one at the top of the stack, I’ll use the Push-Location command or the alias popd
.
I can continue to do this, each time returning to the previous location and removing the current one from the stack until the location stack is empty.
Named Location Stacks
If you’re workflow is a bit more advanced, you can create multiple location stacks by specifying a stack name by adding the -StackName parameter when using the Push, Pop, and Get-Location commands.
Push-Location C:\StorageReports -StackName reports
Get-Location -StackName reports
Pop-Location -StackName reports
Conclusion
Hopefully, these tips are helpful for you when navigating folders using PowerShell; especially in scenarios where using File Explorer is not practical, like when interacting with Windows Server Core.
-
Home
-
Partition Manager
- How to Change Directory in PowerShell Windows 10/11? [Full Guide]
By Ariel | Follow |
Last Updated
How to change directory in PowerShell? A lot of users are confused about the PowerShell change directory command. Don’t worry. This post of MiniTool will walk you through detailed steps to change directory PowerShell on Windows 10/11.
As it’s well known to us all, PowerShell is a practical Windows built-in command-line shell and scripting tool. It allows users to do various works by running specific cmdlets such as PowerShell get user group membership, PowerShell get disk space, PowerShell run exe, PowerShell copy files, PowerShell gets folder size, installing SSH PowerShell, PowerShell unzip/zip files, and more.
However, a great many people are unclear about how to deal with these works in PowerShell. For example, some users from the Stack Overflow community want to change directory in PowerShell but don’t know how to do that. So, here provide a full guide to explain the PowerShell change directory operation.
Change directory in PowerShell. My PowerShell prompts currently pointed to my C drive (PS C:>). How do I change directory to a folder on my Q (PS Q:>) drive? The folder name on my Q drive is «My Test Folder».
https://stackoverflow.com/questions/41130310/change-directory-in-powershell
What Cmdlet Can Be Use to Change Directory in PowerShell
Before you do, it’s necessary to figure out the PowerShell change directory cmdlet first. What command can be used to change directory in PowerShell? After investigating extensive user reports and references, we find that the Get-Location cmdlet is the main way to determine and change the path of your current directory location in PowerShell.
In addition, some other commands can make Windows PowerShell change directory, including the cd, chdir, and sl commands. You can further read the following parts to know more details.
How to Change Directory in PowerShell Windows 10/11
Now, you should have known the PowerShell change working directory commands. Here’s how to change directory PowerShell in Windows 10/11 using these commands.
Tips:
To show you how to make PowerShell change working directory more intuitively, all the following commands are operated assuming that you want to change directory to a folder on the D drive and the folder name on the drive is “Ariel”. Here you can replace the directory with your desired path.
# 1. Windows PowerShell Change Directory Via Set-Location
The first and most common way is to use the Set-Location command. The complete syntax of the command is “Set-Location -Path … -PassThru”. The Path represents the relative or absolute path of the current directory and the PassThru alias enables you to apply the command even if the output is empty. Here’s how to make PowerShell change drive letter using the cmdlet.
Step 1. Press the Win + R keys to open the Run box, and then type powershell in it and hit Enter to open the Windows PowerShell screen.
Step 2. Now, you will find that PowerShell is usually pointing to C:UsersAdministory. To change the current directory to D:Ariel in PowerShell, you can input the following command and hit Enter. Then the output should show you the current working directory of the PowerShell has been changed.
Set-Location -Path D:Ariel -Passthru
# 2. Windows PowerShell Change Directory Via the CD Alias
The cd command serves the same function as the Set-Location cmdlet. It is very simple to operate via the syntax of the “cd -Path … -PassThru” command. To do this work, you just need to type the following command in the PowerShell window and hit Enter. Then the current working directory of your PowerShell will be changed.
cd -Path D:Ariel -PassThru
# 3. Windows PowerShell Change Directory Via the SL Alias
The “sl” cmdlet is another effective way that makes PowerShell navigate to folder. The syntax of the sl command works on the “sl -Path -PassThru”. Here’s an example of how to change directory in PowerShell using the sl command.
sl -Path D:Ariel -PassThru
# 4. Windows PowerShell Change Directory Via the Chdir Alias
In addition to the above commands, you can use the chdir cmdlet which is also an alias of the Set-Location cmdlet. The syntax of the command is “chdir -Path .. -PassThru”. The following example shows you the process of the chdir cmdlet to change the current directory of PowerShell.
chdir -Path D:Ariel -PassThru
Further reading: If you enter some issues like file system corruption and low disk space on Windows 10/11, don’t worry. MiniTool Partition Wizard can help you fix them easily by checking file system errors, extending/resizing partitions, analyzing disk space, upgrading to a larger hard disk, etc.
About The Author
Position: Columnist
Ariel has been working as a highly professional computer-relevant technology editor at MiniTool for many years. She has a strong passion for researching all knowledge related to the computer’s disk, partition, and Windows OS. Up till now, she has finished thousands of articles covering a broad range of topics and helped lots of users fix various problems. She focuses on the fields of disk management, OS backup, and PDF editing and provides her readers with insightful and informative content.