PowerShell - List files and directories using Get-ChildItem

by
Jeremy Canfield |
Updated: April 26 2023
| PowerShell articles
The Get-ChildItem command without any options will list the files and directories in the present working directory.
> Get-ChildItem
Directory: C:\Users\john.doe
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 3/26/2021 4:02 AM my-directory
-a---- 8/1/2022 11:37 PM 11 example.txt
The -recurse flag can be used to list the files and directories below the present working directory.
> Get-ChildItem -recurse
The -path option can be used to specify the base directory to search from.
> Get-ChildItem -path 'C:\temp' -recurse
The -filter option can be used to only return files and directories that match the filter.
> Get-ChildItem -path 'C:\temp' -recurse -filter '*.txt'
The Format-Table filter can be used to just return the file names.
> Get-ChildItem | Format-Table Name -HideTableHeaders
my-directory
example.txt
Here is how you could loop over each file.
$files = Get-ChildItem -file
foreach ($file in $files) {
Write-Output $file.FullName
}
Sometimes, permission denied will be returned at certain sub directories. The -erroraction silentlycontinue option can be used to ignore such errors.
> Get-ChildItem -recurse -erroraction silentlycontinue
The -file flag can be used to only display files.
> Get-ChildItem -file
Did you find this article helpful?
If so, consider buying me a coffee over at