Get the items and child items in a folder or registry key. (dir / ls / gci)
Syntax
Get-ChildItem [ [-path] string[] | [-literalPath] string[] ]
[[-filter] string] [-include string[]] [-exclude string[]] [-name]
[-recurse] [-force] [-UseTransaction] [CommonParameters]
Key
-path string
The paths to the items from which content is to be retrieved.
Wildcards are permitted. Default is the current directory (.)
-literalPath string
Like Path above, only the value is used exactly as typed.
No characters are interpreted as wildcards. If the path includes any
escape characters then enclose the path in single quotation marks.
-include string
Include only the specified items from the Path. e.g. "May*"
-exclude string
Omit the specified items from the Path e.g. "*SS64*"
-filter string
A filter in the provider's format or language.
The exact syntax of the filter (wildcard support etc) depends on the provider.
Filters are more efficient than -include/-exclude, because the provider
applies the filter when retrieving the objects, rather than having
PowerShell filter the objects after they are retrieved.
-name
Retrieve only the names of the items.
This is useful when piping the names of the child items to another command.
-recurse
Get the items plus all child items of the location(s).
Only for paths that point to a container such as C:\Windows or C:\Windows\*
A path such as *.txt will not have any child items.
-force
Get all items including hidden or system files, but will not override
security/file permissions.
-UseTransaction
Include the command in the active transaction.
CommonParameters:
-Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
-OutBuffer -OutVariable.
When listing files and sub-directories, get-childitem will return the mode (attributes), last write time, file size (length), and the filename.
Valid modes (attributes) are: d (directory), a (archive), r (read-only), h (hidden), and s (system).
The default path is the current directory ' . '
To specify all the items in the current directory use '*'
Wildcards
When listing a single folder (without recursion), you can do
get-childitem c:\music\*.mp3
Unlike the CMD shell, in PowerShell the path filter of c:\music\*.mp3 is applied only to files not folders (or other containers)
The way
to apply a wildcard recursively to a whole tree of items in PowerShell is to use the -include parameter:
get-childitem c:\music\ -include *.mp3 -recurse
This change in syntax was required because some providers (such as the registry provider) allow backslashes in a value name, separating the -path from the -include string makes it possible to use get-childitem against any provider: files, registry, processes etc.
Examples
Get the child items in the current location:
PS C:\> get-childitem
List all the .XLS files in a folder and all sub-folders:
PS C:\> get-childitem \\Server64\Work\ -Include *.xls -Recurse
List all the .XLS files in a folder:
PS C:\> get-childitem \\Server64\Work\* -Include *.xls
List all the files owned by BillG:
PS C:\> PS C:\> get-childitem C:\Work\ -recurse | get-acl | where {$_.Owner -match "BillG"}
Get a list of sub-folder names and store in $folders
PS C:\> $folders = gci 'C:\YourDir' | Where-Object{($_.PSIsContainer)} | foreach-object{$_.Name}
Measure the size of a folder:
PS C:\> Get-ChildItem C:\Work\ -Recurse -Force |
Measure-Object -property length -sum
Get all the certificates in the certificate store, use the dynamic parameter -codesigningcert to get only certificates with code-signing authority. ( see "get-help certificate" for more)
PS C:\> get-childitem cert:\. -recurse -codesigningcert
“You [humans] think that you an insignificant, while there is a great universe contained in you” - Ali ibn Abi Talib
Related:
Get-Item - Get a file object or get a registry (or other namespace) object
Get-Location - Display the current location
Join-Path -resolve - Combine a path and child-path
© Copyright SS64.com 1999-2013
Some rights reserved