PowerShell Get-ChildItem Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Set-ItemProperty cmdlet in PowerShell.
https://cloudaffaire.com/powershell-set-itemproperty-cmdlet/
In this blog post, we will discuss Get-ChildItem cmdlet in PowerShell. You can use Get-ChildItem cmdlet to list the content of a directory or registry hive. Get-ChildItem supports arguments like ‘depth’ and ‘Attributes’ which allows you to control the amount and type of data that you want to get. Get-ChildItem cmdlet doesn’t display empty directories.
By default, Get-ChildItem lists the mode (Attributes), LastWriteTime, file size (Length), and the Name of the item. The letters in the Mode property can be interpreted as follows:
- l (link)
- d (directory)
- a (archive)
- r (read-only)
- h (hidden)
- s (system)
Get-ChildItem Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## Get-ChildItem ## [[-Path] ## [[-Filter] ## [-Include ## [-Exclude ## [-Recurse] ## [-Depth ## [-Force] ## [-Name] ## [-UseTransaction] ## [-Attributes ## [-Directory] ## [-File] ## [-Hidden] ## [-ReadOnly] ## [-System] ## [ |
Get-ChildItem Cmdlet Argument List:
- –Attributes: Gets files and folders with the specified attributes. This parameter supports all attributes and lets you specify complex combinations of attributes. The Attributes parameter supports the following properties:
- Archive: This file is marked to be included in the incremental backup operation.
- Compressed: The file is compressed.
- Device: Reserved for future use.
- Directory: The file is a directory.
- Encrypted: The file or directory is encrypted. For a file, this means that all data in the file is encrypted. For a directory, this means that encryption is the default for newly created files and directories.
- Hidden: The file is hidden, and thus is not included in an ordinary directory listing.
- IntegrityStream: The file or directory includes data integrity support.
- Normal: The file is a standard file that has no special attributes.
- NoScrubData: The file or directory is excluded from the data integrity scan.
- NotContentIndexed: The file will not be indexed by the operating system’s content indexing service.
- Offline: The file is offline. The data of the file is not immediately available.
- ReadOnly: The file is read-only.
- ReparsePoint: The file contains a reparse point, which is a block of user-defined data associated with a file or a directory.
- SparseFile: The file is a sparse file. Sparse files are typically large files whose data consists of mostly zeros.
- System: The file is a system file. That is, the file is part of the operating system or is used exclusively by the operating system.
- Temporary: The file is temporary.
- –Depth: This parameter was added in PowerShell 5.0 and enables you to control the depth of recursion.
- –Directory: To get a list of directories, use the Directory parameter or the Attributes parameter with the Directory property.
- –Exclude: Specifies, as a string array, a property or property that this cmdlet excludes from the operation. The value of this parameter qualifies the Path parameter.
- –File: To get a list of files, use the File parameter. You can use the Recurse parameter with File.
- –Filter: Specifies a filter to qualify the Path parameter.
- –Force: Allows the cmdlet to get items that otherwise can’t be accessed by the user, such as hidden or system files.
- –Hidden: To get only hidden items, use the Hidden parameter or the Attributes parameter with the Hidden property.
- –Include: Specifies, as a string array, an item or items that this cmdlet includes in the operation. The value of this parameter qualifies the Path parameter.
- –LiteralPath: Specifies a path to one or more locations. The value of LiteralPath is used exactly as it’s typed. No characters are interpreted as wildcards.
- –Name: Gets only the names of the items in the location. The output is a string object that can be sent down the pipeline to other commands.
- –Path: Specifies a path to one or more locations. Wildcards are accepted.
- –ReadOnly: To get only read-only items, use the ReadOnly parameter or the Attributes parameter ReadOnly property.
- –Recurse: Gets the items in the specified locations and in all child items of the locations.
- –System: Gets only system files and directories.
- –UseTransaction: Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
PowerShell Get-ChildItem Cmdlet:
List Contents Of A Directory In PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
######################################### ## PowerShell | Cmdlet | Get-ChildItem ## ######################################### ## PowerShell Latest Version (5) ## create some files, directories and registry keys New-Item -Path HKCU:\Software -Name MyKey New-Item -Path HKCU:\Software\MyKey -Name MyKey1 -Value "a" New-Item -Path HKCU:\Software\MyKey -Name MyKey2 -Value "b" New-Item -ItemType Directory -Path C:\MyDir\ New-Item -ItemType Directory -Path "C:\MyDir\MyDir1","C:\MyDir\MyDir2" New-Item -ItemType Directory -Path C:\MyDir\MyDir3\MySubDir1\MySubDir2\MySubDir3 New-Item -ItemType File -Path "C:\MyDir\MyFile1.txt", "C:\MyDir\MyFile2.txt" New-Item -ItemType File -Path "C:\MyDir\MyDir1\MyFile1.txt","C:\MyDir\MyDir2\MyFile2.txt" New-Item -ItemType File -Path C:\MyDir\MyDir3\MySubDir1\MySubDir2\MySubDir3\MyFile3.txt ## get content of a directory in PowerShell Get-ChildItem -Path C:\MyDir |
List Only Directories In PowerShell:
1 2 3 |
## list only directories in PowerShell Get-ChildItem -Path C:\MyDir -Directory |
List Only Files In PowerShell:
1 2 3 |
## list only files in PowerShell Get-ChildItem -Path C:\MyDir -File |
List Only Specific Types Of Files Or Files Having Specific Extension In PowerShell:
1 2 3 |
## list only specific types of files in PowerShell Get-ChildItem -Path C:\MyDir -Name "*.txt" |
List All The Files And Directories Up To A Certain Depth In PowerShell:
1 2 3 |
## specify depth with Get-ChildItem cmdlet to retrieve all the files Get-ChildItem -Path C:\MyDir -Depth 4 |
Get All The Sub Keys Under A Registry Key In PowerShell:
1 2 3 4 5 6 7 8 |
## list registry subkeys in PowerShell Get-ChildItem -Path HKCU:\Software\MyKey ## remove the items created for this demo Remove-Item -Path HKCU:\Software\MyKey -Recurse Remove-Item -Path C:\MyDir -Recurse -Force |
Hope you have enjoyed this article. In the next blog post, we will discuss Get-Location cmdlet in PowerShell.
To Set more details on PowerShell, kindly follow below official documentation