PowerShell is the next-generation command line interface for Windows-based operating systems. While using PowerShell we generally need to change directories to run different commands or list different directory contents. PowerShell provides the cd
and Set-Location
commands in order to change directories. In this tutorial, we examine different scenarios to change the directory in PowerShell like changing to an absolute path, changing to the parent directory, changing to the relative directory, etc.
cd Command
The cd
command name comes from change directory
. The cd command is very popular in different platforms and operating systems as well as in Ms-DOS. Linux distributions like Debian, Ubuntu, Fedora, CentOS, etc. provide the cd command. The Windows MS-DOS also supports the cd command to change the directory. The cd command provides very basic usage.
cd PATH
Set-Location Command
Even the cd command provides the ability to change the directory in PowerShell the Set-Location
command is provided as the native command for PowerShell. Similar to the cd command we can provide the path or directory we want to change to the Set-Location command as a parameter. In the following example, we change to the “C:\Users”.
PS> Set-Location C:\Users
Change Relative Directory
We can change to the relative directory using the Set-Location command in PowerShell. In the following example, we change to the directory named “ismail”.
PS> Set-Location ismail
Alternatively, we can specify the multiple level relative directory to change. In the following example, we change to the directory “ismail/Desktop”.
PS> Set-Location ismail\Desktop
Change Absolute Directory
We can use absolute paths r absolute directories in order to change the current working directory. We provide the directory name from the start which starts with the drive name etc. In the following example, we navigate to the “C:\Users\ismail\Desktop”.
PS> Set-Location C:\Users\ismail\Desktop
Change To Parent Directory
We can change to the parent directory by using ..
location specifiers. Two dots means upper directory or parent directory. In the following example, we change to the parent directory.
PS> Set-Location ..
Change To D: Directory
We can change to another drive like D:
or E:
etc.. by using the Set-Location command.
PS> Set-Location D: