Table of Contents
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.
1 2 3 |
Get-ChildItem -Path "E:\Test" -Filter "file*" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 4/25/2023 7:22 AM File -a---- 3/10/2023 12:42 AM 33629 file - Copy.doc -a---- 3/11/2023 10:31 PM 165792 file - Copy.pdf -a---- 3/10/2023 12:42 AM 33629 file.docx -a---- 3/11/2023 10:31 PM 165792 file.pdf -a---- 4/6/2023 5:50 AM 168 file.ps1 -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 4/16/2023 9:49 PM 48898 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt -a---- 3/7/2023 8:16 AM 57 file4.txt -a---- 3/28/2023 12:18 PM 70 filessss.txt -a---- 3/28/2023 12:18 PM 54 filez.txt |
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.
1 2 3 |
Get-ChildItem -Path "E:\Test" -Filter "file*" -File |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/10/2023 12:42 AM 33629 file - Copy.doc -a---- 3/11/2023 10:31 PM 165792 file - Copy.pdf -a---- 3/10/2023 12:42 AM 33629 file.docx -a---- 3/11/2023 10:31 PM 165792 file.pdf -a---- 4/6/2023 5:50 AM 168 file.ps1 -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 4/16/2023 9:49 PM 48898 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt -a---- 3/7/2023 8:16 AM 57 file4.txt -a---- 3/28/2023 12:18 PM 70 filessss.txt -a---- 3/28/2023 12:18 PM 54 filez.txt |
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.
Further reading:
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.
1 2 3 |
Get-ChildItem -Path "E:\Test" -Filter "file*" -File -Recurse |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/10/2023 12:42 AM 33629 file - Copy.doc -a---- 3/11/2023 10:31 PM 165792 file - Copy.pdf -a---- 3/10/2023 12:42 AM 33629 file.docx -a---- 3/11/2023 10:31 PM 165792 file.pdf -a---- 4/6/2023 5:50 AM 168 file.ps1 -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 4/16/2023 9:49 PM 48898 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt -a---- 3/7/2023 8:16 AM 57 file4.txt -a---- 3/28/2023 12:18 PM 70 filessss.txt -a---- 3/28/2023 12:18 PM 54 filez.txt Directory: E:\Test\Sample Files Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 3/28/2023 4:00 PM 73 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt Directory: E:\Test\Sample Files\FolderA Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt |
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.
1 2 3 |
Get-ChildItem -Path "E:\Test" -Filter "file*.txt" -File -Recurse |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 4/16/2023 9:49 PM 48898 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt -a---- 3/7/2023 8:16 AM 57 file4.txt -a---- 3/28/2023 12:18 PM 70 filessss.txt -a---- 3/28/2023 12:18 PM 54 filez.txt Directory: E:\Test\Sample Files Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 3/28/2023 4:00 PM 73 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt Directory: E:\Test\Sample Files\FolderA Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt |
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.
1 2 3 |
Get-ChildItem -Path "E:\Test" -Filter "file*.txt" -File -Recurse -Depth 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 4/16/2023 9:49 PM 48898 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt -a---- 3/7/2023 8:16 AM 57 file4.txt -a---- 3/28/2023 12:18 PM 70 filessss.txt -a---- 3/28/2023 12:18 PM 54 filez.txt Directory: E:\Test\Sample Files Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 3/28/2023 4:00 PM 73 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt |
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.
1 2 3 4 |
Get-ChildItem -Path "E:\Test" -Recurse | Where-Object {$_.Name -eq "file.txt"} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt Directory: E:\Test\Sample Files Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt Directory: E:\Test\Sample Files\FolderA Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt |
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 toSilentyContinue
. Remember, theGet-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.
1 2 3 |
Get-ChildItem . "file*.txt" -r -d 1 | ? {!$_.PSIsContainer} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 4/16/2023 9:49 PM 48898 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt -a---- 3/7/2023 8:16 AM 57 file4.txt -a---- 3/28/2023 12:18 PM 70 filessss.txt -a---- 3/28/2023 12:18 PM 54 filez.txt Directory: E:\Test\Sample Files Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 3/28/2023 4:00 PM 73 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt |
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 to1
.|
: It is used to forward the output of theGet-ChildItem
cmdlet to the next process?
: It is an alias of theWhere-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.
1 2 3 4 |
dir -Path "E:\Test" -Recurse | Where-Object {$_.Name -eq "file.txt"} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt Directory: E:\Test\Sample Files Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt Directory: E:\Test\Sample Files\FolderA Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt |
Use the gci
alias of Get-ChildItem
with the Where-Object
cmdlet to find the file by exact name and extension in PowerShell.
1 2 3 4 |
gci -Path "E:\Test" -Recurse | Where-Object {$_.Name -eq "file.txt"} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt Directory: E:\Test\Sample Files Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt Directory: E:\Test\Sample Files\FolderA Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt |
Use the ls
alias of Get-ChildItem
with the Where-Object
cmdlet to find the file by exact name and extension in PowerShell.
1 2 3 4 |
ls -Path "E:\Test" -Recurse | Where-Object {$_.Name -eq "file.txt"} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt Directory: E:\Test\Sample Files Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt Directory: E:\Test\Sample Files\FolderA Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt |
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 theGet-ChildItem
; don’t forget to replaceGet-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.
1 2 3 |
Get-Item "E:\Test\file.txt" |
1 2 3 4 5 6 7 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt |
We can also use wildcard characters while writing the path with the Get-Item
cmdlet; see the following example.
1 2 3 |
Get-Item "E:\Test\file*.txt" |
1 2 3 4 5 6 7 8 9 10 11 12 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 4/16/2023 9:49 PM 48898 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt -a---- 3/7/2023 8:16 AM 57 file4.txt -a---- 3/28/2023 12:18 PM 70 filessss.txt -a---- 3/28/2023 12:18 PM 54 filez.txt |
The Get-Item
cmdlet results can also be piped to the next cmdlet, as shown below.
1 2 3 |
Get-Item "E:\Test\file*.txt" | Where-Object{$_.Name -eq "filez.txt"} |
1 2 3 4 5 6 7 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 12:18 PM 54 filez.txt |
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.
1 2 3 |
Get-ChildItem -Path "E:\Test" | Where-Object { $_.Name -clike "file*"} |
1 2 3 4 5 6 7 8 9 10 11 12 |
Directory: E:\Test Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 3/28/2023 3:59 PM 652 file.txt -a---- 4/16/2023 9:49 PM 48898 file1.txt -a---- 3/28/2023 3:39 PM 23 file2.txt -a---- 3/7/2023 8:16 AM 57 file4.txt -a---- 3/28/2023 12:18 PM 70 filessss.txt -a---- 3/28/2023 12:18 PM 54 filez.txt |
That’s all about PowerShell find file by name.