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.

If you are looking to get all files in current directory in PowerShell, you can use below command:

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.

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.

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.

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.

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

Was this post helpful?

Leave a Reply

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