In PowerShell, the directory can be retrieved recursively using the Get-ChildItem cmdlet and the -Recurse parameter.
|
1 2 3 |
Get-ChildItem -Path "D:\Content Writing" -Recurse |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Directory: D:\Content Writing Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 09-04-2023 16:57 test1 d----- 09-04-2023 16:58 test2 Directory: D:\Content Writing\test1 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 09-04-2023 16:57 7 file1.md -a---- 09-04-2023 16:57 7 file2.md -a---- 09-04-2023 16:57 7 file3.md Directory: D:\Content Writing\Directory\test2 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 09-04-2023 16:57 7 file4.md -a---- 09-04-2023 16:57 7 file5.md |
If you are looking to get all files in current directory in PowerShell, you can use below command:
|
1 2 3 |
Get-ChildItem -Recurse |
The folders and files can also be excluded using the -Exclude parameter.
First, have a look at the purpose of the parameters and cmdlets that will be used for different purposes in this article:
- The
Get-ChildItemcmdlet in PowerShell retrieves a recursive directory and file list. -Recurseis used to retrieve the directory recursively, meaning all the files, folders, and subfolders will be retrieved.- Use the
-Excludeparameter to exclude specific files and folders. You can modify the-Excludeparameter to include multiple file and folder names by separating them with commas - The
Select-Objectcmdlet only displays theFullNameproperty of each item, which is the full path to the file or folder.
Let’s dive into the examples to learn the use of each cmdlet and the parameters discussed above.
Using Get-ChildItem Cmdlet
We can use the Get-ChildItem cmdlet to do the following:
- To retrieve the directory recursively
- To exclude a specific file from a directory
- To exclude a particular folder from a directory
- To exclude files of the same type or extension from a directory
See the following examples to learn.
Use Get-ChildItem with the -Recurse parameter and Select-Object cmdlet to retrieve a directory recursively.
|
1 2 3 |
Get-ChildItem -Path "D:\Content Writing" -Recurse | Select-Object FullName |
|
1 2 3 4 5 6 7 8 9 10 11 |
FullName -------- D:\Content Writing\test1 D:\Content Writing\test2 D:\Content Writing\test1\file1.md D:\Content Writing\test1\file2.md D:\Content Writing\test1\file3.md D:\Content Writing\test2\file4.md D:\Content Writing\test2\file5.md |
Use Get-ChildItem with -Recurse & -Exclude parameters and Select-Object cmdlet to retrieve the complete folder and its subfolders recursively but excluding the file1.txt file.
|
1 2 3 |
Get-ChildItem -Path "D:\Content Writing" -Recurse -Exclude "file1.md" | Select-Object FullName |
|
1 2 3 4 5 6 7 8 9 10 |
FullName -------- D:\Content Writing\test1 D:\Content Writing\test2 D:\Content Writing\test1\file2.md D:\Content Writing\test1\file3.md D:\Content Writing\test2\file4.md D:\Content Writing\test2\file5.md |
Use Get-ChildItem with -Recurse & -Exclude parameters and Select-Object cmdlet to retrieve the complete folder and its subfolders recursively but excluding the test2 folder.
|
1 2 3 |
Get-ChildItem -Path "D:\folder" -Recurse -Exclude "test2" | Select-Object FullName |
|
1 2 3 4 5 6 7 8 9 10 |
FullName -------- D:\Content Writing\test1 D:\Content Writing\test1\file1.md D:\Content Writing\test1\file2.md D:\Content Writing\test1\file3.md D:\Content Writing\test2\file4.md D:\Content Writing\test2\file5.md |
The above command will exclude the specified folder, not its subfolders or files. As you can see, we have retrieved the location for the
file4.docxandfile5.docxfiles from thetest2folder but not thetest2folder itself.
Use Get-ChildItem with -Recurse & -Exclude parameters and Select-Object cmdlet to retrieve the complete folder and its subfolders recursively but excluding the files of the same type or extension.
|
1 2 3 |
Get-ChildItem -Path "D:\Content Writing" -Recurse -Exclude *.md | Select-Object FullName |
|
1 2 3 4 |
D:\Content Writing\test1 D:\Content Writing\test2 |
That’s all about how to get all files in directory recursively in PowerShell.