How to search for a specific extension using Get-ChildItem in PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To get the output of the get-childitem with a specific extension, we need to use –includeparameter.

Example

In the below example, we will use the below script to get only .xml files.

Get-ChildItem D:\Temp\ -Recurse -Include *.xml

Output

PS C:\WINDOWS\system32> Get-ChildItem D:\Temp\ -Recurse -Include *.xml
    Directory: D:\Temp\GPO_backup\{C9C3DB4C-2E51-4201-B3C3-7C0F1ACECBE9}
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       24-11-2018     11:34           6215 Backup.xml
-a----       24-11-2018     11:34            602 Bkupinfo.xml
    Directory: D:\Temp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       07-05-2018     23:00            301 cars.xml

You can filter multiple extensions with include parameter and for that, you need to add a comma (,) between extensions.

Example

For example, we will get the output here with file extensions xml and csv.

Get-ChildItem D:\Temp\ -Recurse -Include *.xml, *.csv

Output

PS C:\WINDOWS\system32> Get-ChildItem D:\Temp\ -Recurse -Include *.xml, *.csv
    Directory: D:\Temp\GPO_backup\{C9C3DB4C-2E51-4201-B3C3-7C0F1ACECBE9}\DomainSysvol\GPO\Machine\microsoft\windows
    nt\Audit
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       24-11-2018     11:34           6234 audit.csv
    Directory: D:\Temp\GPO_backup\{C9C3DB4C-2E51-4201-B3C3-7C0F1ACECBE9}
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       24-11-2018     11:34           6215 Backup.xml
-a----       24-11-2018     11:34            602 Bkupinfo.xml
    Directory: D:\Temp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       07-05-2018     23:00            301 cars.xml
-a----       25-10-2017     08:13            104 testcsv.csv
raja
Published on 23-Jan-2020 12:17:07
Advertisements