0
0
PowerShellscripting~5 mins

Get-ChildItem for listing in PowerShell

Choose your learning style9 modes available
Introduction

Get-ChildItem helps you see files and folders inside a folder. It is like looking inside a drawer to find what is there.

You want to see all files in your Documents folder.
You need to check what pictures are saved in a folder.
You want to find all scripts in a project folder.
You want to list all folders inside a main folder.
You want to see files with a certain extension, like .txt or .jpg.
Syntax
PowerShell
Get-ChildItem [-Path] <string> [-Filter <string>] [-Recurse] [-File] [-Directory]

-Path tells where to look. If you skip it, it looks in the current folder.

-Recurse means look inside all folders inside the folder, not just the top level.

Examples
Lists all files and folders in the current folder.
PowerShell
Get-ChildItem
Lists all files and folders inside the Documents folder.
PowerShell
Get-ChildItem -Path C:\Users\YourName\Documents
Lists only files ending with .txt in the current folder.
PowerShell
Get-ChildItem -Path . -Filter *.txt
Lists all files inside the Projects folder and all its subfolders.
PowerShell
Get-ChildItem -Path C:\Projects -Recurse -File
Sample Program

This script first shows all files and folders in the current folder. Then it shows only folders. Finally, it lists all PowerShell script files (.ps1) inside the current folder and all folders inside it.

PowerShell
Write-Host "Files and folders in current directory:";
Get-ChildItem;

Write-Host "\nOnly folders in current directory:";
Get-ChildItem -Directory;

Write-Host "\nAll .ps1 files in current directory and subdirectories:";
Get-ChildItem -Filter "*.ps1" -Recurse -File;
OutputSuccess
Important Notes

Get-ChildItem runs fast for small folders but can be slower with many files or deep folders when using -Recurse.

Use -File to get only files, and -Directory to get only folders.

Common mistake: forgetting to use quotes around paths with spaces, like "C:\My Folder".

Summary

Get-ChildItem lists files and folders in a folder.

You can filter by file type or look inside subfolders with -Recurse.

Use -File or -Directory to list only files or only folders.