How to List Directories in PowerShell?

If you are a PowerShell expert, you must understand the importance of working with files and directories. Recently, I got a requirement to display directories presented using PowerShell. PowerShell provides a few commands to work directories and subdirectories. In this tutorial, I will explain how to list directories using PowerShell with some examples.

I will show you the different scenarios you will most likely encounter.

List Directories in the Current Location using PowerShell

To list directories in the current location in PowerShell, you can use the Get-ChildItem cmdlet with the -Directory switch. Here’s an example:

Get-ChildItem -Directory

This command will display all the directories in the current location. For instance, if you are in the “C:\Users\Bijay\Documents” directory, it will list all the subdirectories within the “Documents” folder.

I executed the script in my macOS and you can see the exact output in the screenshot below. It displays all the directories presented in the current directory.

List Directories in PowerShell

Check out Create a Shortcut to a Folder Using PowerShell

Listing Directories in a Specific Location

If you want to list directories in a specific location, you can provide the path as a parameter to the Get-ChildItem cmdlet. For example:

Get-ChildItem -Path "C:\Users\Bijay\Music" -Directory

This command will list all the directories within the “Music” folder of the user “Bijay”. This is the path of a Windows operating system.

Here is how it looks like in a macOS.

Get-ChildItem -Path "/Users/bijay/Downloads" -Directory

Here is the exact output in the screenshot below:

How to List Directories in PowerShell

Check out Find and Remove Empty Folders Using PowerShell

Recursively Listing Directories

To list directories recursively, including all subdirectories, you can use the -Recurse switch with the Get-ChildItem cmdlet. Here’s an example:

Get-ChildItem -Path "C:\Users\Bijay\Downloads" -Directory -Recurse

This command will list all the directories and subdirectories within the “Downloads” folder of the user “Bijay”. It will traverse through all levels of subdirectories and provide a complete directory structure.

Formatting the Output

By default, the Get-ChildItem cmdlet displays the directory names in a simple list format. However, you can customize the output using various formatting cmdlets. For instance, to display the directory names in a table format, you can use the Format-Table cmdlet:

Get-ChildItem -Directory | Format-Table -AutoSize

This command will display the directory names in a table format, automatically adjusting the column widths based on the content.

Filter Directories

PowerShell allows you to filter directories based on specific criteria using the Where-Object cmdlet. For example, to list only the directories that start with the letter “P”, you can use the following command:

Get-ChildItem -Directory | Where-Object { $_.Name -like "P*" }

This command will list only the directories whose names start with the letter “P”.

Read Rename a Folder in PowerShell if It Exists

Real-World Scenario and Example

Let’s consider a real-world scenario where I encountered an issue and used PowerShell to list directories for troubleshooting.

Problem: Locating a Specific Directory

I was working on a project that required me to locate a specific directory within a complex file system structure. The directory name started with “Project_”, followed by a unique identifier. I needed to find this directory quickly to access the necessary files.

Solution: Using PowerShell to List Directories

To solve this problem, I used PowerShell to list directories recursively and filter the results based on the directory name pattern. Here’s the command I used:

Get-ChildItem -Path "C:\Projects" -Directory -Recurse | Where-Object { $_.Name -like "Project_*" }

This command recursively listed all the directories within the “C:\Projects” folder and filtered the results to include only the directories whose names started with “Project_”. The output displayed the matching directory names, along with their full paths, making it easy for me to locate the specific directory I needed.

Conclusion

In this tutorial, we explored how to list directories using PowerShell. We covered listing directories in the current location, specifying a specific path, and recursively listing subdirectories. We also learned how to format the output and filter directories based on certain criteria.

If you still have any questions feel free to leave me a comment below.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.