PowerShell Get-Childitem -Exclude

PowerShell GCI -Exclude Parameter

If you want to refine the output of Get-ChildItem (GCI), then consider the -Exclude parameter.  This technique seems more reliable than using PowerShell's -Include parameter.

Topics for Get-ChildItem -Exclude

 ♣

Example 1: To Exclude Particular Items

Scenario: You want a list of files, but wish to eliminate particular names, and thus have a more focussed inventory.  Take a trivial example: we need a list of SystemRoot\System32 files, but we don't want to see any NLS (national language service) files.

# PowerShell -Exclude example
$FileSource = "$env:SystemRoot\system32"
$List = Get-ChildItem -Path $FileSource -Exclude *nls*
$List
Write-Host 'Number of files: ' $List.count

Note 1: I have introduced three variables to make it easier to amend my example.  $env:SystemRoot usually corresponds to C: \Windows.

Note 2: The .count property makes it easier to see the effect of changing values for -Exclude.

Example 2: To Exclude More Than One Item

Here is how to add more items to exclude using the simple comma.

# PowerShell example to exclude multiple items
Clear-Host
$FileSource = "$env:SystemRoot\system32"
$List = Get-ChildItem -Path $FileSource -Exclude *nls*, *audio*, *xa*
$List | Sort-Object Name
Write-Host "Number of files: " $List.count

Note 4: I like to surround the excluded letters with *wildcards*,  for multiple items separate them with separate with a comma.  For no particular reason I sorted the list of files.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds' Network Performance Monitor will help you discover what's happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds' Network Performance Monitor

Example 3: Excluding Using a Where-Object Clause

Where-Object is no longer my first choice for filtering data; instead I seek out faster, or technically superior constructions.  However, you cannot beat the  versatility of its conditional operators; in this case 'notMatch'.

Clear-Host
$FileSource = "$env:SystemRoot\system32"
$List = GCI -Path $FileSource | Where-Object {$_.Name -NotMatch "nls"}
Write-Host "Number of files: " $List.count

Note 5:  The Where-Object clause allows through all files that don't match the letters "nls".

Note 6: To save word-wrapping the third line I substituted the alias GCI for Get-ChildItem.

Which Technique is Faster, Where-Object or -Exclude?

I bet that -Exclude would be faster.  Let us call for PowerShell's Measure-Command to see if my prediction was correct.

Experiment 1: Measuring the Speed of Where-Object

Clear-Host
Measure-Command {
$FileSource = "$env:SystemRoot\system32"
$List = Get-ChildItem -Path $FileSource | Where-Object {$_.Name -NotMatch "nls"}
}
Write-Host "Number of files: " $List.count

Result: 410 milliseconds.  I use the fastest of 3 runs to allow for bias due to caching.

Experiment 2: Measuring the Speed of -Exclude

Clear-Host
Measure-Command {
$FileSource = "$env:SystemRoot\system32"
$List = Get-ChildItem -Path $FileSource -Exclude *nls*
}
Write-Host "Number of files: "$List.count

Result: 625 milliseconds (fastest of 3 runs)

I was shocked that using PowerShell's Where-Object was faster than the -Exclude parameter.  See more examples of Measure-Command.

Creating an Array of -Exclude Items

Employing lots of variables is rarely the road to writing the tightest code; however, variables are helpful when troubleshooting errors or tweaking scripts.  When working with -Exclude I often wish to extend the list of text strings, and it makes it easier to read if I assign this list to a variable.  I was annoyed that no matter of tweaking the speech marks, or punctuation could get this to work:

$Eliminate = "*nls*", "*audio*", "*xa*"

Fortunately, there was a solution, and that was to create a simple array thus:

$Eliminate = @("*nls*", "*audio*", "*xa*")

# PowerShell -Exclude with @(array)
Clear-Host
$FileSource = "$env:SystemRoot\system32"
$Eliminate = @("*nls*", "*audio*", "*xa*")
$List = Get-ChildItem -Path $FileSource -Exclude $Eliminate
$List | Sort-Object Name
Write-Host "Number of files: " $List.count

Note 7: Observe how @( introduces the array, and how each element is enclosed a double quote.  The separator for the elements is the comma.

Further Research on Get-ChildItem

Get-Help for Get-ChildItem

# Research PowerShell Get-Childitem Parameters
Get-Help Get-Childitem -Full

Note 8: Get-Help reveals useful parameters such as -Include, -Force, and the most useful -Recurse.

Note 9: Here is function I created to extend what Get-ChildItem can achieve.  PowerShell Get-File function.

Guy Recommends: SolarWinds Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

Encouraging computers to sleep when they're not in use is a great idea - until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses for example, rousing machines so that they can have update patches applied.  My real reason for recommending you download this free tool is because it's so much fun sending those 'Magic Packets'. Give WOL a try - it's free.

Download your free copy of SolarWinds Wake-On-LAN

Research PowerShell Commands which Use the -Exclude Parameter

# PowerShell script to research parameters
Get-Command | Where { $_.parameters.keys -Contains "Exclude"}

Discover More Members of PowerShell's  "-Item" Family

# Members of PowerShell's Item Family
Clear-Host
Get-Command -Noun *Item

Results
--------------
Copy-Item
Get-ChildItem (Basics)
Get-Item (gi)
Invoke-Item
Move-Item
New-Item
Remove-Item
Set-Item

Compare with PowerShell's -Include parameter »

Summary of Get-ChildItem -Exclude

When you need to improve the output of Get-ChildItem, then the -Exclude parameter enables you to filter the output.  For multiple items consider using an array.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell file tutorials:

PowerShell Home   • Add-Content   • Get-Content   • Set-Content  • PowerShell -Filter   • Test-Path

PowerShell Get-ChildItem   • Get-ChildItem -Include   • Get-ChildItem -Exclude   • Compare-Object

PowerShell Registry  • Get-Credential  • PowerShell ItemProperty  • PowerShell ItemPropery GCI

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

 

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2017 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: