PowerShell Find File by Name

PowerShell find filename by name

Using Get-ChildItem Cmdlet

Use the Get-ChildItem cmdlet with -Path and -Filter parameters to find the specified files and directories in the given path in PowerShell.

The Get-ChildItem retrieves items and child items from one or multiple paths (locations). The -Path parameter is used to specify one or multiple paths to locations; we can also use wildcard characters while using this parameter. The default location for the -Path parameter is the current directory (.).

The -Filter parameter filters files and directories from the specified location based on the wildcard or pattern. Note that the -Filter parameter is faster than the -Exclude and -Include parameters because it filters on the server side rather than the client side. Moreover, it means the PowerShell provider performs the filtering; for instance, FileSystem provider instead of PowerShell engine.

The above command searched for all the files and directories whose name starts with the word file in the E:\Test path; the * wildcard character means at least one or more characters.

Use the Get-ChildItem Cmdlet with the -File Parameter

Use the Get-ChildItem cmdlet with -Path, -Filter, and -File parameters to retrieve files only from the specified path in PowerShell.

This time, the command retrieved all the files whose names started with the word file. Remember, the -File parameter is available in the FileSystem provider only.

In the above examples, we got files from the E:\Test path, but we can use the -Recurse parameter to retrieve all the files from the given directory and its subdirectories; see the following section.

Use Get-ChildItem Cmdlet with -Recurse Parameter

Use the Get-ChildItem cmdlet with -Path, -Filter, -File, and -Recurse parameters to retrieve files only from the specified directory and its subdirectories in PowerShell.

We got all the files whose name starts with the word file from E:\Test, E:\Test\Sample Files, and E:\Test\Sample Files\FolderA directories. Therefore, we can map the -Filter parameter to file*.txt to get all .txt files whose names start with the word file; see the following example.

Look at the directories in the above output. E:\Test is the directory, E:\Test\Sample Files is the first level of subdirectories, and E:\Test\Sample Files\FolderA is the second level of subdirectories. Now, think of a situation where we want to find files by name from the directory and first level of subdirectories. How can we do it? We need to use the -Depth parameter as follows.

This time, we did not get a text file from the second level of subdirectories, which was E:\Test\Sample Files\FolderA.

Use Get-ChildItem with Where-Object

Use the Get-ChildItem cmdlet with the Where-Object cmdlet to find the file by exact name and extension in PowerShell.

We have already learned about the Get-ChildItem cmdlet, -Recurse parameter, and levels of directories. Here, we used Where-Object to iterate over the collection forwarded by Get-ChildItem via pipeline (|). For each item, we retrieved its name using the Name property and compared it with the "file.txt" filename using the -eq operator. If it were equal, it would be included in the output; otherwise, not.

If you want to continue with the specified command regardless of errors, then you can use the -ErrorAction parameter and set its value to SilentyContinue. Remember, the Get-ChildItem does not get hidden files; use the -Force parameter to retrieve them.

Use Get-ChildItem with PSIsContainer

Use Get-ChildItem with PSISContainer to find a file by name in the given directory in PowerShell.

This example is the more straightforward solution due to its simplicity of finding a file by name in PowerShell. How? Let me explain it below:

  • Get-ChildItem: We have already learned it in previous sections; it is used to retrieve items and child items from one or multiple locations.
  • .: It maps to the positional parameter -Path, and we have already learned that the default directory of the -Path parameter is the current directory represented by .. You must be in the directory where you want to search file(s) by name.
  • "file*.txt": It is the pattern for the required file name.
  • -r: It maps to the -Recurse parameter.
  • -d 1: It maps to the -Depth parameter whose value is set to 1.
  • |: It is used to forward the output of the Get-ChildItem cmdlet to the next process
  • ?: It is an alias of the Where-Object, which we have already learned.
  • {!$_.PSIsContainer}: It is a condition which filters all container items, such as directories.

Use Get-ChildItem Aliases

The PowerShell includes dir and gci aliases for all platforms, while ls is for Windows only. Let’s learn each of them below.

Use the dir alias of Get-ChildItem with the Where-Object cmdlet to find the file by exact name and extension in PowerShell.

Use the gci alias of Get-ChildItem with the Where-Object cmdlet to find the file by exact name and extension in PowerShell.

Use the ls alias of Get-ChildItem with the Where-Object cmdlet to find the file by exact name and extension in PowerShell.

If you don’t know the aliases for a particular cmdlet, you can use the Get-Alias -Definition Get-ChildItem command to get available aliases. In this command, we got all aliases for the Get-ChildItem; don’t forget to replace Get-ChildItem with your cmdlet if you are looking for a different PowerShell cmdlet.

Using Get-Item Cmdlet

Use the Get-Item cmdlet to find a file by name in the specified location in PowerShell. We use this cmdlet where searching different levels of subdirectories is not required. It means the -Recurse and -Depth parameters will not work with this cmdlet.

We can also use wildcard characters while writing the path with the Get-Item cmdlet; see the following example.

The Get-Item cmdlet results can also be piped to the next cmdlet, as shown below.

PowerShell Find File by Name [Case sensitive]

In case, you want to find file by name with case sensitive, here are the steps:

  • Use Get-ChildItem to retrieves the items.
  • Use where-object with clike operator to do case sensitive string comparison.

That’s all about PowerShell find file by name.

Was this post helpful?

Leave a Reply

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