Home ยป How to List Directories in PowerShell

How to List Directories in PowerShell

To list directories within a directory using PowerShell, you can use the Get-ChildItem cmdlet with the -Directory parameter.

You can use the following method to list directories (folders) in a directory.

Method 1: List directories within a directory using PowerShell

# specify the path to the directory you want to list
$directoryPath = "C:\temp\log"

# List the directories in the specified directory
Get-ChildItem -Path $directoryPath -Directory

This example returns the list of directories within a directory.

The following example shows how to do it with syntax.

List Directories Using PowerShell

To list directories (folder) within a directory, you can use the Get-ChildItem cmdlet along with its -Directory parameter. You can also use Get-ChildItem cmdlet aliases such as ls or dir.

The following example shows how to do it.

# specify the path to the directory you want to list
$directoryPath = "C:\temp\log"

# List the directories in the specified directory
Get-ChildItem -Path $directoryPath -Directory

# List the directories using ls
ls -Path $directoryPath -Directory

# List the directories using dir
dir -Path $directoryPath -Directory

Output:

PowerShell list directories
PowerShell list directories

In this example, the Get-ChildItem cmdlet uses the -Path parameter to specify the directory path $directoryPath. It lists only directories using the -Directory parameter.

You can also use the Get-ChildItem cmdlet aliases ls or dir to list only directories in a specified directory.

Conclusion

I hope the above article on listing directories using PowerShell is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.