
Table of contents
- Introduction to PowerShell Change Directory
- How to change the Directory in PowerShell?
- Changing the drive in PowerShell
- Setting the default working directory in PowerShell
- Push-Location and Pop-Location
- Viewing the Current Directory in PowerShell
- Tips and tricks for using PowerShell Change Directory Effectively
- Common pitfalls and errors when changing directories in PowerShell
- Wrapping up
Introduction to PowerShell Change Directory
The CD command is an alias of the Set-Location cmdlet used to navigate through the file system and change the working directory. It takes a directory name as an argument and changes the working directory to that directory. We can use either relative or absolute path names as arguments to navigate to a specific directory.
To change the current directory in PowerShell, you can use the Set-Location cmdlet, also known as the cd alias. Here is how to CD in PowerShell:
If the directory name contains spaces, you can enclose the folder name in double quotes like this:
Changing to a parent directory
To navigate to the parent directory of the current working directory, you can use the double-dot (..) notation, like this:
Similarly, Instead of moving one level up, you can move two levels by appending a backslash (\) at the end of each.
To reach the root drive, use:
This will set the current working directory to the root drive, such as “C:\”.
Changing to a child directory
The dot (.) represents the current working directory, and we use the backslash (\) as the directory separator. When you execute this command, you change the current working directory to the “Reports” folder.
PowerShell to Navigate to a Folder
To change the directory in PowerShell, you need to provide the path to the desired folder. This path can be either an absolute path (starting from the file system’s root) or a relative path (relative to the current working directory). You can use an absolute path to navigate to a specific folder. An absolute path specifies the entire path from the root directory to the target directory. For example, to navigate to the “Documents” directory on the C: drive, type:
CD C:\Users\username\Documents
Changing to any other folder is possible with Set-Location by passing the path parameter to it:
Set-Location -Path C:\Scripts
You can also use relative paths to navigate to a directory. A relative path specifies the path from the current working directory to the target directory. For example, to navigate to a directory named “Data” that is located in the “Documents” directory, you can use a relative path like this:
Auto-Completion: Similar to other shells, PowerShell supports auto-completion. Start typing the path and press Tab to complete folder or file names. This tab completion is especially useful when dealing with long directory names.
Using environment variables
Set-Location ${env:ProgramFiles}This changes the working directory to “C:\Program Files”.
Set-Location (Join-Path -Path $HOME -ChildPath "Documents") # or cd (Join-Path -Path $HOME -ChildPath "Documents")
Changing the drive in PowerShell
You can also use PowerShell to navigate through different drives on your computer. To navigate to a different drive, you can use the Set-Location cmdlet with the Drive parameter like this:
- Open the PowerShell console.
- Type the drive letter followed by a colon like this – D:
- Press Enter to change to the specified drive.
- You can now navigate through the file system on that drive using the CD command.
Similarly, you can navigate to a folder in another drive using:
Set-Location -Path D:\Scripts #Also works: CD D:\Scripts

In addition, add the “-PassThru” parameter to return the path after PowerShell changes the current working directory. Changing to network drives is also possible. E.g.,
Set-Location \\Fileserver\Public
Provide the UNC path to Set-Location or cd.
Setting the default working directory in PowerShell
# Check if profile exists
if (-not (Test-Path $profile)) { # Create profile if it doesn't exist New-Item -Type File -Path $profile -Force Write-Host "Profile created at $profile" -ForegroundColor Green
} else { Write-Host "Profile already exists at $profile" -ForegroundColor Yellow
}
# Set the desired default directory
$DefaultDirectory = "C:\Scripts"
# Append the Change-Location command to the profile
Add-Content -Path $profile -Value "Change-Location $DefaultDirectory"
Write-Host "Default directory set to $defaultDirectory" -ForegroundColor Green
# Inform user to reload their profile or restart PowerShell
Write-Host "Please reload your profile or restart PowerShell for changes to take effect." -ForegroundColor CyanThe working directory should now be set to the directory you specified.
Push-Location and Pop-Location
You can also the Push-Location cmdlet (alias pushd) to temporarily change to a different directory and then use the Pop-Location cmdlet (alias popd) to return to the previous directory. This can be useful if you need to perform some tasks in a different directory and then return to the previous location when you are done.
# Change to the D:\Scripts directory Push-Location D:\Scripts # Perform some tasks here # Return to the previous directory Pop-Location
Here is how it works:

Viewing the Current Directory in PowerShell
If you’re not sure which directory you’re currently in, you can use the command:
You can also use the alias “pwd”, which stands for “print working directory” and will display the current directory path on the command line.
Setting the working directory to the script location 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.
Set-Location -LiteralPath $PSScriptRoot
Here, the $PSScriptRoot variable represents the path to the script’s location. By executing this command, the working directory will be set to the script’s location, allowing you to access files and folders relative to the script. You can also get the current path of the script using the automatic variable “$Script:MyInvocation.MyCommand.Path”. E.g.,
#Get path of your current directory location $CurrentPath = Split-Path $Script:MyInvocation.MyCommand.Path -Parent #Get Files and folders in current path Dir $CurrentPath
Tips and tricks for using PowerShell Change Directory Effectively
Now that you know how to use the CD command to navigate through the file system in PowerShell, here are some tips and tricks to help you use it more effectively:
- Use the Tab key to autocomplete the directory and file names. This will save you time and reduce the risk of typos.
- Use the Up Arrow key to quickly recall and reuse previous commands.
- Use the Push-Location command to save the current directory to a stack and navigate to a new directory. You can then use the Pop-Location command to return to the previous directory.
- Use aliases: PowerShell provides aliases for common commands, including
cd. You can use aliases to save typing time and streamline your workflow. For example, instead of typingcd, you can use the aliasslorchdirto change directories. - Use wildcard characters: PowerShell supports wildcard characters, such as
*and?, for pattern matching. You can use wildcard characters to navigate to folders or files with similar names. For example, to change to a folder that starts with “Proj” you can use the following command: “CD Proj*”.
In addition to the basic CD command, PowerShell provides several advanced techniques for changing directories. Here are some of the most useful techniques:
- Use the Join-Path command to combine multiple path components into a single path.
- Use the Resolve-Path command to resolve the full path of a file or directory, even if it contains relative path components.
- Use the Split-Path command to split a path into its parent and child components.
- Use the Test-Path command to test whether a file or directory exists at a specified path.
Common pitfalls and errors when changing directories in PowerShell
While changing directories in PowerShell, you may encounter some common pitfalls and errors. Understanding these pitfalls can help you avoid potential issues and troubleshoot problems effectively. Here are some common pitfalls to watch out for:
- Directory Does Not Exist: If you get an error indicating the directory does not exist, check your spelling and path.
- Invalid characters: PowerShell has specific rules regarding valid characters in folders and file names. If a folder or file name contains invalid characters, you may encounter errors when attempting to change directories. Make sure to use valid characters and escape special characters, if necessary.
- Permissions issues: Some folders or files may have restricted permissions, preventing you from accessing or changing directories. Ensure you have the necessary permissions to navigate to the desired folders or files.
- Missing drives: If you attempt to change to a drive that does not exist or is not currently mounted, you will encounter errors. Make sure to verify the existence and availability of drives before attempting to change them.
Wrapping up
How do I change the current directory in PowerShell?
How do I change from C drive to D drive in PowerShell?
How to change the path in PowerShell?
How do I change the directory in PowerShell when there is a space?
To change the directory in PowerShell, when there is a space in the path, you can enclose the path in double quotes (“”). For example, if the directory is “C:\Program Files”, you would use the command: cd “C:\Program Files”
How do I run a PowerShell script in a specific directory?
How do I change the default directory in PowerShell?
To change the default directory in PowerShell, you can create a PowerShell profile and set the default directory in it. For example, if you want to set the default directory to “C:\Scripts”, you would add “Set-Location C:\Scripts” in the profile file (Create one, if it doesn’t exist already: New-Item -path $profile -type file –force).
How do I navigate a directory in Windows PowerShell?
How do I navigate to a parent directory?
To navigate to a parent directory, you can use the .. symbol. For example: Set-Location ..cd ..
This will move you one level up from the current directory.
How can I quickly navigate to my home directory?
How can I check the current directory in PowerShell?
To check the current directory in PowerShell, you can use the Get-Location cmdlet or its alias pwd. For example: Get-Location
How do I list all directories in PowerShell before changing to a new one?
To list all directories in PowerShell, you can use the dir or ls command to view all directories and files in your current location.
<# ::
@echo off & setlocal
powershell -NoProfile Start-Process powershell -Verb RunAs -ArgumentList '-NoExit -WindowStyle Normal -NoProfile -ExecutionPolicy Bypass Set-Location \\\"%~dp0\\"; $batFilePath = \\\"%~f0\\\"; Invoke-Expression (Get-Content -Raw \\\"%~f0\\\")'
exit /b
#>
# PowerShell code goes below; it is executed in the context of an
# elevated PowerShell session based on the `powershell` call above.
[pscustomobject] @{ batFilePath = $batFilePath workingDir = $PWD
}That is, the above creates an elevated, visible, stay-open PowerShell instance that demonstrates that (a) the working directory is the same as the caller’s and (b) that variable $batFilePath reflects the original batch file’s full path.
%~f0and%~dp0are used to refer to the calling batch file’s full file path and the full directory path in which it resides, usingcmd.exe‘s argument-expansion syntax, as described in the output fromcmd /c call /?The PowerShell CLI, with
-Command/-c, which is the implied parameter in Windows PowerShell‘s CLI,powershell.exe, requires"chars. to be escaped as\"in order to be passed through verbatim as part of the code.- By contrast,
pwsh, the PowerShell (Core) 7 CLI defaults to-Fileand requires explicit use of-Command/-cin order to pass PowerShell source code as an argument. The same escaping rules apply there, thoughpwsh.exeon Windows also accepts""as an escaped", which is actually preferable when calling fromcmd.exe(batch files) so as to avoid running afoul ofcmd.exe‘s syntax.
- By contrast,
From Wikipedia, the free encyclopedia
| Original author(s) | Bill Joy |
|---|---|
| Developer(s) | Various open-source and commercial developers |
| Operating system | Unix, Unix-like, DOS, Windows, ReactOS |
| Platform | Cross-platform |
| Type | Command |

The directory stack underlies the functions of these two commands. It is an array of paths stored as an environment variable in the CLI, which can be viewed using the command dirs in Unix or Get-Location -stack in PowerShell. The current working directory is always at the top of the stack.
In Windows PowerShell, pushd is a predefined command alias for the Push-Location cmdlet and popd is a predefined command alias for the Pop-Location cmdlet. Both serve basically the same purpose as the pushd and popd commands.
/etc
Microsoft Windows and ReactOS
C:\Users
CMD batch file
offrem This batch file deletes all .txt files in a specified directory *.txt All text files deleted in the directorypushd [path | ..]
pathThis optional command-line argument specifies the directory to make the current directory. Ifpathis omitted, the path at the top of the directory stack is used, which has the effect of toggling between two directories.
popd



