How To Work With Filesystems And Directories In PowerShell

How to work with directories and filesystem in PowerShellHow to work with directories and filesystem in PowerShell

Key Points

  • To list items within the current directory in PowerShell, use “Get-ChildItem,” or use this command followed by the complete path of the directory to list all items using the absolute path.
  • Use “Set-Location -Path” followed by absolute path to change the directory to the specified folder, or go up the directory levels using “cd ..”, “cd ..\..\”, or “cd \”.

Efficiently managing directories and navigating through the filesystem is crucial as vast amounts of data are generated and organized. PowerShell emerges as a powerful tool that makes interacting with directories seamless and enables effortless manipulation of files and folders.

Whether you are an experienced IT professional or a curious tech enthusiast, this guide aims to simplify the complexities of working with directories and the filesystem in PowerShell. In this article, we show you, with examples, how to navigate through and work with files and folders using the PowerShell command line.

How to Open Windows PowerShell

There are multiple ways through which you can open the PowerShell application in your system.

You can access PowerShell in Windows 10 or Windows 11 by simply right-clicking on the Windows icon in the taskbar or pressing the “Win + X” keys and selecting “Windows PowerShell.” If you want to run it as an administrator, click on “Windows PowerShell (Admin).”

Open Windows PowerShell from the WinX menu
Open Windows PowerShell from the WinX menu

Alternatively, you can also open PowerShell from the Run Command box.

  1. Press the Windows Key + R to open the Run Command box.

  2. Type in “PowerShell” and press Enter.

    Open PowerShell via the run command box
    Open PowerShell via the run command box

To launch PowerShell as an administrator, press Ctrl + Shift + Enter after typing “PowerShell” in the Run Command box.

Working with Directories and Filesystem in PowerShell

Directories, or folders, help you organize your computer files. They act as containers where you can put your files and even other folders.

Whereas, the filesystem in Windows is like a big map that shows you where everything is stored on your computer. It’s like a blueprint that helps you navigate through all your files and folders. The filesystem decides how things are organized and makes sure everything is in the right place.

Let us now continue to show you how to perform specific tasks relating to files and folders using just PowerShell.

Change Directory to a Specific Path in PowerShell

In PowerShell, you can switch to a specified directory by using the “Set-Location” command.

Use the following command to switch the working (current) directory using the following “-Path” parameter:

Set-Location -Path "[FullPath]"

Note: If the path contains spaces, enclose it in double quotation marks.

For example, to change to the “C:\Users” directory, use this command:

Set-Location -Path "C:\Users"
Change to different directory/folder in PowerShell
Change to a different directory/folder in PowerShell

You can also switch between different directories simply by entering the drive letter followed by a colon, like in this example:

Change to a different drive in PowerShell
Change to a different drive in PowerShell

As you can see, no PowerShell-specific command is needed.

Change Directory using Environmental Variables in PowerShell

In Windows, there are two main directories for programs: \Program Files (x86) and \Program Files. To switch to these directories in PowerShell, it’s best to use their environment variables. This way, you do not need to provide complete paths.

Environment variables are like shortcuts that represent specific paths on your computer. By calling these variables in PowerShell, you can easily navigate to the corresponding directories without typing out the full paths.

For example, to change to the “C:\Program Files (x86)” directory, you can use the environment variable “%ProgramFiles(x86)%”. Similarly, to switch to the “C:\Program Files” directory, you can use the “%ProgramFiles%” variable. Following are the steps to do it:

Note: The environment variable for the “Program Files (x86)” folder is {env:ProgramFiles(x86)} and for the “Program Files” folder is {env:ProgramFiles}.

Use the following syntax to change the directory to a folder with an environmental variable:

Set-Location $[EnvironmentalVariable]
Change directory in PowerShell using Environmental Variable
Change the directory in PowerShell using Environmental Variable

You can use the same concept with all directories that have an environmental variable.

Move a File, Folder with PowerShell

If you want to move a certain file or folder via PowerShell, use the “Move-Item” command. With this command, you can move a single file or folder, or even specify the types of multiple files to move. Of course, for this purpose, you need to use both the source path and the destination path.

The command syntax for moving a single file or folder is:

Move-Item -Path "[SourcePath]" -Destination "[DestinationPath]"

Replace [SourcePath] with the path of the file/folder you want to move, and [DestinationPath] with the path where you want to move it. If it is a file that you are moving, also specify its file extension.

Move a file or folder using PowerShell
Move a file or folder using PowerShell

In the example above, we have moved the “TestFile.txt” from D drive to the root of E drive.

To move a specific file type, we will use the “*” (asterisk) followed by the file extension. this will move all files within the particular directory to the destination path. Here is the command’s syntax:

Move-Item -Path "[SourcePath]\*.[FileExtension]" -Destination "[DestinationPath]"

Replace [SourcePath] with the path of the folder from where you want to move the files, [FileExtension] with the file type you want to move, and [DestinationPath] with the path where you want to move them.

Move multiple files with same file type using PowerShell
Move multiple files with the same file type using PowerShell

Copy a File, Folder with PowerShell

Above, we discussed how to move a file, multiple files, or a folder using PowerShell. Let us now show you how to copy them to another location. The “Copy-Item” cmdlet is used for this purpose.

Use the syntax below to copy a file or a folder to a new location using PowerShell:

Copy-Item -Path "[SourcePath]" -Destination "[DestinationPath]"

Replace [SourcePath] with the path of the file/folder you want to copy, and [DestinationPath] with the path where you want to copy it. If it is a file that you are moving, also specify its file extension.

Copy a file or folder using PowerShell
Copy a file or folder using PowerShell

In this example, we copied the “TestFile1.txt” from the DestinationFolder in the E drive to the SourceFolder in the D drive.

You can use the same syntax to copy complete folders as well. Of course, a file extension won’t be needed in that case.

You can also rename the file being copied during the process. Simply provide the new name in the [DestinationPath] variable, and the file will be copied with the specified name. Here’s an example where we renamed the file “TestFile1.txt” to “NewTestFile.txt”.

Copy a file while renaming it using PowerShell
Copy a file while renaming it using PowerShell

You can also copy all contents of a folder into another directory using the “*” (asterisk) using the command syntax:

Copy-Item -Path "[SourcePath]\*" -Destination "[DestinationPath]"
Copy all items from source folder to target folder using PowerShell
Copy all items from the source folder to the target folder using PowerShell

To simplify the process of navigating to a specific location in your file system, you can store the full path of a directory in a variable. Using variables allows for easy access to frequently used directories, eliminating the need to type the full path multiple times or rely on location history. Here are the steps to follow:

  1. Store a directory path to a custom variable by using the following command:

    $[VariableName]='[PathToStore]'

    Replace [VariableName] with any custom name for the variable, and [PathToStore] with the complete path of the file/folder you want to store.

    Store a filefolder location inside a variable in PowerShell
    Store a file/folder location inside a variable in PowerShell
    Set-Location -Path $psh
  2. Now use this variable instead of complete paths.

    You can use this variable in the copy-item command, move-item command, or set-location command. Here are some examples:

    Set-Location -Path $[VariableName]
    Copy-Item -Path "D:\SourceFolder\TestFile1.txt" -Destinaton $[VariableName]
    Move-Item -Path "D:\SourceFolder\*.txt" -Destinaton $[VariableName]

Moving Up Levels within Directory Hierarchy in PowerShell

When managing files or working on projects in PowerShell, it is common to navigate to the parent directory of the current directory. The CD (Change Directory) command in PowerShell supports the “..” notation, which allows you to easily switch to the parent directory without specifying the full path.

This feature simplifies moving up one level in the file system hierarchy and facilitates efficient directory navigation.

To move up one level in the directory hierarchy, enter the following command:

cd ..

The space after the command is optional but improves readability. This prompt will change to the parent directory without specifying the full path.

To move up two levels in the directory hierarchy, run the command:

cd ..\..\

Lastly, to navigate to the root directory of the current drive, enter the following command:

cd \
Move update directory levels in PowerShell
Move update directory levels in PowerShell

List Items in a Specific Directory in PowerShell

To list the contents of a directory in PowerShell, the “Get-ChildItem” cmdlet is used. You can either provide the absolute path of the directory or simply use Get-ChildItem when you are already in the same directory.

In this example, we will be listing the content of the E:\Documents directory.

When running the Get-ChildItem command from outside this directory, the absolute path of the directory should be specified. This ensures that PowerShell knows exactly which directory to retrieve the contents from. The command will be:

Get-ChildItem E:\Documents\
List all items in directory in PowerShell using absolute path
List all items in the directory in PowerShell using the absolute path

When executing the Get-ChildItem command directly within a directory, there is no need to specify the full path. This is known as a relative path. In such cases, you can simply run the command without providing the absolute path, and PowerShell will display the contents of the current directory. The command will be:

Get-ChildItem
List all items in current directory in PowerShell
List all items in the current directory in PowerShell

Conclusion

Using PowerShell to manage directories and files offers numerous benefits to users. It is versatile and provides easy-to-use commands that simplify the process of working with directories and navigating the file system.

Whether it is about effortlessly changing directories, accessing files with spaces in their names, or efficiently organizing and manipulating files, PowerShell proves to be a valuable tool for it all.

If you liked this post, Share it on:
Yamna is a highly skilled researcher with a specialization in Biotechnology. She draws on her extensive knowledge, experience and ability to research concepts in depth to produce writings that demystify complex technology-related topics.

Get Updates in Your Inbox

Sign up for the regular updates and be the first to know about the latest tech information

Talk to us now

Talk to us straight and get your questions answered right away

Tell Us About Your Project
Web Dev Service Contact Form (Popup)