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).” Alternatively, you can also open PowerShell from 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: Note: If the path contains spaces, enclose it in double quotation marks. For example, to change to the “C:\Users” directory, use this command: You can also switch between different directories simply by entering the drive letter followed by a colon, like in this example: 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: 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: 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. 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: 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.
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: 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. 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”. You can also copy all contents of a folder into another directory using the “*” (asterisk) using the command syntax:
Navigate to a Variable Directory in 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:
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. Replace [VariableName] with any custom name for the variable, and [PathToStore] with the complete path of the file/folder you want to store. You can use this variable in the copy-item command, move-item command, or set-location command. Here are some examples: 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: 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: Lastly, to navigate to the root directory of the current drive, enter the following command:
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: 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:
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.