Home » How to List Contents of Directory in PowerShell

How to List Contents of Directory in PowerShell

To list the contents of a directory using PowerShell, you can use the Get-ChildItem cmdlet. This command lists both files and directories within a directory using PowerShell.

The following methods can be used for directory listing in PowerShell.

Method 1: List the Directory Content in PowerShell

# using the Get-ChildItem
Get-ChildItem

# Using the 'ls' alias
ls

# Using the 'dir' alias

dir

This example returns the list of all files and directories in the current directory.

Method 2: List the files and directories in a different directory without changing the current directory

# specify the directory path
$directoryPath = "C:\temp\log"

Get-ChildItem -Path $directoryPath

This example returns the list of files and directories in a different directory without changing the current directory.

The following examples show how to list the directory content in PowerShell.

List the Directory Content in PowerShell

To list the directory content in PowerShell, you can use the Get-ChildItem cmdlet or its alias ls or dir.

The following example shows how to do it with syntax.

Get-ChildItem

Output:

List the contents of directory in PowerShell
List the contents of the directory in PowerShell

In the above output, it displays the list of contents (files and directories) in the current directory. You can also use the Get-ChildItem alias such as ls or dir to list all items in a current directory.

List the Files and Directories in Different Directory Without Changing the Current Directory

You can use the Get-ChildItem cmdlet in PowerShell along with its -Path parameter to specify the directory path.

The following example shows how to do it with syntax.

# specify the different directory path
Get-ChildItem -Path "C:\temp\log"

Output:

PowerShell list files and directories without changing the current directory
PowerShell lists files and directories without changing the current directory

In the above example, the Get-ChildItem cmdlet uses the -Path parameter to specify the path to the directory and list all files and directories without changing the current directory.

Conclusion

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

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