How to Use Get-ChildItem in PowerShell for File Listings

PowerShell is a powerful scripting language and command-line shell designed especially for system administrators. One of the most commonly used cmdlets in PowerShell is Get-ChildItem, which allows users to list files and folders in a directory. Whether you’re managing files on a local computer or across a network, understanding how to leverage Get-ChildItem can boost your efficiency dramatically.

At first glance, Get-ChildItem might seem like a basic command, similar to dir in the traditional Command Prompt. However, it comes with a wide array of parameters that make it incredibly versatile and powerful.

Basic Usage

To start, simply typing the following command in a PowerShell window will list all files and folders in the current directory:

Get-ChildItem

This returns a collection of objects representing files and folders, which you can further manipulate or filter. To look at a specific directory, supply a path:

Get-ChildItem "C:\Users\Public"

Exploring Options and Parameters

PowerShell provides numerous parameters to customize the output of Get-ChildItem. Here are some useful ones:

  • -Recurse: Lists all files and directories in the specified location and all subdirectories.
  • -File: Displays only files, excluding directories.
  • -Directory: Displays only directories, excluding files.
  • -Filter, -Include, and -Exclude: Allow filtering by file name or extension.
  • -Hidden: Includes hidden items in the result.

Below is an example that displays all `.txt` files in a specific folder and its subfolders:

Get-ChildItem "C:\Logs" -Filter *.txt -Recurse

You can also combine these parameters for more targeted results. For instance:

Get-ChildItem "C:\Projects" -Recurse -File -Include *.ps1

This command finds all PowerShell script files in the Projects directory and all its subfolders.

Working With Output

One of PowerShell’s greatest strengths is treating the output as objects. This means you can pass the output of Get-ChildItem to other cmdlets using the pipeline (|) to perform additional operations:

Get-ChildItem -Path "C:\Data" -Recurse -File | Where-Object {$_.Length -gt 1MB}

This command filters for files larger than 1 MB in the C:\Data folder and its subdirectories.

Want to sort files by size?

Get-ChildItem -Path C:\Videos -File | Sort-Object Length -Descending

This will arrange all video files in descending order of their size.

Using Get-ChildItem Remotely

When combined with PowerShell remoting, you can use Get-ChildItem to explore file systems on remote machines:

Invoke-Command -ComputerName "Server01" -ScriptBlock { Get-ChildItem "D:\Logs" -Recurse }

Just ensure PowerShell remoting is enabled on the target machine using Enable-PSRemoting.

Practical Use Case: Cleaning Up Old Files

Here’s a real-world scenario—removing all files older than 90 days:

Get-ChildItem "C:\Temp" -Recurse -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-90) } | Remove-Item

This is especially useful for system administrators managing temporary directories or log files that pile up over time.

Tips and Best Practices

  • Always test your commands using -WhatIf before deletion or modification operations. Example:
  • Remove-Item -Path "C:\Temp\*" -WhatIf
  • Use aliases sparingly: While gci is a common alias for Get-ChildItem, writing out the full command improves readability and reduces confusion in shared scripts.
  • Use output formatting: You can format the output to display only specific properties.
Get-ChildItem | Select-Object Name, Length, LastWriteTime

Conclusion

From simple file listings to advanced recursive searches and even automation of cleanup tasks, Get-ChildItem is a fundamental cmdlet every PowerShell user should master. Its flexibility makes it an ideal tool for managing files and directories efficiently across local and remote environments.

With just a few lines of code, you can gain deep insights into your file system and even automate complex file management workflows—putting the real power in PowerShell.