Get ChildItem - PowerShell
Overview
Get-ChildItem retrieves a collection of child items (files, folders, symlinks, etc.) contained within a specified path or drive. It enables users to explore the contents of directories, list files, and perform various operations on the retrieved items.
Syntax
Get-ChildItem [-Path] <ItemPath> [[-Credential] <PSCredential>] [-Filter] <String> [-ErrorAction] <Action>
[-Exclude] <String[]> [-Include] <String[]> [-Recurse] [-Force] [-Hidden] [-NotHidden] [-LiteralPath] <String>
[-Depth <UInt32>] [-Directory] [-File] [-Link] [-SkipHidden] [-SkipReadOnly] [-AcceptPipelineInput]
[-PassThru] [-ExcludeSimpleItems] [-ExcludeFilter] <String[]> [-NoClobber] [-WhatIf] [<CommonParameters>]
Options/Flags
- -Path
: Specifies the path to the directory or drive from which to retrieve child items. - -Credential
: Provides credentials for accessing the specified path if necessary. - -Filter
: Filters the results to only include items that match the specified pattern. - -ErrorAction
: Controls how errors are handled. - -Exclude <String[]>: Excludes child items matching the specified patterns.
- -Include <String[]>: Includes only child items matching the specified patterns.
- -Recurse: Retrieves child items from all subdirectories recursively.
- -Force: Overwrites existing items if necessary.
- -Hidden: Includes hidden items in the results.
- -NotHidden: Excludes hidden items from the results.
- -LiteralPath
: Interprets the specified path as a literal path, bypassing alias resolution. - -Depth
: Specifies the maximum depth of recursion when -Recurse is used. - -Directory: Only retrieves directories.
- -File: Only retrieves files.
- -Link: Only retrieves symbolic links.
- -SkipHidden: Excludes hidden items from the results, even when -Hidden is specified.
- -SkipReadOnly: Excludes read-only items from the results.
- -AcceptPipelineInput: Accepts pipeline input as the source path.
- -PassThru: Returns the retrieved child items as output objects.
- -ExcludeSimpleItems: Excludes simple items (drives, files, directories) from the results.
- -ExcludeFilter <String[]>: Excludes items based on the specified filter criteria.
- -NoClobber: Prevents overwriting existing items.
- -WhatIf: Performs a simulation of what would happen without actually modifying anything.
Examples
Example 1: List all files in the current directory
Get-ChildItem
Example 2: Retrieve all subdirectories in “C:\Users” recursively
Get-ChildItem -Path "C:\Users" -Recurse -Directory
Example 3: Filter files by extension and name
Get-ChildItem -Path "C:\Documents" -Filter "*.txt" -Include "*Important*"
Example 4: Pass child items to a pipeline
Get-ChildItem | Format-Table -AutoSize
Common Issues
- Access denied: Ensure you have sufficient permissions to access the specified path.
- Path not found: Verify that the specified path exists and is accessible.
- Unexpected results: Check the filter and exclusion criteria to ensure they align with your expectations.
Integration
Get-ChildItem can be integrated with other commands to perform various tasks:
- List files and their properties:
Get-ChildItem -Path "C:\Users" | Select-Object Name, LastWriteTime, Length
- Search for files in multiple directories:
(Get-ChildItem -Path "C:\Users\*\Documents").FullName