Get All Files in Directory Recursively in PowerShell

Get all files in directory reclusively in PowerShell

In PowerShell, the directory can be retrieved recursively using the Get-ChildItem cmdlet and the -Recurse parameter.

Get-ChildItem -Path "D:\Content Writing" -Recurse

    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:

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-ChildItem cmdlet in PowerShell retrieves a recursive directory and file list.
  • -Recurse is used to retrieve the directory recursively, meaning all the files, folders, and subfolders will be retrieved.
  • Use the -Exclude parameter to exclude specific files and folders. You can modify the -Exclude parameter to include multiple file and folder names by separating them with commas
  • The Select-Object cmdlet only displays the FullName property 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.

Get-ChildItem -Path "D:\Content Writing" -Recurse | Select-Object FullName
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.

Get-ChildItem -Path "D:\Content Writing" -Recurse -Exclude "file1.md" | Select-Object FullName
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.

Get-ChildItem -Path "D:\folder" -Recurse -Exclude "test2" | Select-Object FullName
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.docx and file5.docx files from the test2 folder but not the test2 folder 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.

Get-ChildItem -Path "D:\Content Writing" -Recurse -Exclude *.md | Select-Object FullName
D:\Content Writing\test1
D:\Content Writing\test2

That’s all about how to get all files in directory recursively in PowerShell.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.