PowerShell: List Files

By Xah Lee. Date: . Last updated: .

List Files

List file in current directory. Any of the following

Get-ChildItem
Get-ChildItem .
Get-ChildItem -path .

Get-ChildItem has alias dir, ls, and gci.

Specify Path

# list file of a given path
Get-ChildItem c:/Users/xah/web/

Get-ChildItem -path c:/Users/xah/web/

If path contain space, need quote. Example: "c:/Users/xah/Saved Games/"

Show hidden files

Get-ChildItem -Force

recursive

# list file in current dir, and all subdirs
Get-ChildItem -Recurse

Show File Path Only

# list file, show just file path (as string)
Get-ChildItem -Name
# note: the beginning drive letter and dirs are not shown

List Files by File Name Pattern

by File Name Extension:

Get-ChildItem -Recurse -include *html

Emacs backup files (file name ending in ~):

Get-ChildItem -Recurse -Include *~

Search file name by regex:

# list jpg or png files

# search file name by regex
Get-ChildItem -Recurse -name | Select-String -Pattern "jpg|png"

# search file name by multiple literal string
Get-ChildItem -Recurse -name | Select-String -Pattern jpg,png -SimpleMatch

List Empty Files

Get-ChildItem -recurse | where {$_.length -eq 0}

List Only Files, No Directory

Get-ChildItem -File -Recurse

Count Number of Files

# count number of files in current dir and subdirs
Get-ChildItem -Recurse | Measure-Object
# count number of html files in current dir and subdirs
Get-ChildItem -Recurse -Include *html  | Measure-Object

List File Creation Time

# list file with creation time
Get-ChildItem | Sort-Object CreationTime | Format-Table Name, CreationTime
# list files whose creation date is greater than x
Get-ChildItem | Where-Object { $_.CreationTime -gt [datetime]"2014/05/28" } | Sort-Object CreationTime | Format-Table Name, CreationTime

Delete File by Wildcard Pattern

# delete macOS folder preference file
Get-ChildItem -Force -Recurse -Include .DS_Store | Remove-Item -Force
# delete emacs backup. file name ending in ~
Get-ChildItem -Recurse -Include *~ | Remove-Item

PowerShell

How-to

Advanced

Script Examples