How to exclude specific extension in Get-ChildItem in PowerShell?



To exclude specific extensions, you need to use –Exclude parameter in Get-ChildItem.

Example

For example, as shown below when we run command, it will exclude .html files and display the rest.

Get-ChildItem D:\Temp\ -Recurse -Exclude *.html

Output

Directory: D:\Temp Mode                LastWriteTime         Length Name ----                -------------         ------ ---- d-----       24-11-2018     11:31                LGPO     Directory: D:\Temp\LGPO Mode                LastWriteTime         Length Name ----                -------------         ------ ---- -a----       01-06-2017     03:22         410088 LGPO.exe -a----       01-06-2017     02:25         638115 LGPO.pdf     Directory: D:\Temp Mode                LastWriteTime         Length Name ----                -------------         ------ ---- -a----       07-05-2018     23:00            301 cars.xml -a----       08-12-2017     10:16            393 style.css -a----       08-12-2017     11:29           7974 Test.xlsx -a----       25-10-2017     08:13            104 testcsv.csv

You can exclude multiple extensions as well.

Example

In the below example, we will exclude html and xml extensions.

Get-ChildItem D:\Temp\ -Recurse -Exclude *.html,*.xml 

Output

Directory: D:\Temp Mode                LastWriteTime         Length Name ----                -------------         ------ ---- d-----       24-11-2018     11:31                LGPO     Directory: D:\Temp\LGPO Mode                LastWriteTime         Length Name ----                -------------         ------ ---- -a----       01-06-2017     03:22         410088 LGPO.exe -a----       01-06-2017     02:25         638115 LGPO.pdf     Directory: D:\Temp Mode                LastWriteTime         Length Name ----                -------------         ------ ---- -a----       08-12-2017     10:16            393 style.css -a----       08-12-2017     11:29           7974 Test.xlsx -a----       25-10-2017     08:13            104 testcsv.csv
Updated on: 2020-01-23T07:49:27+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements