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
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
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.
Here is how to add more items to exclude using the simple comma.
# PowerShell example to exclude multiple items
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.
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.
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'.
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.
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
Result: 410 milliseconds. I use the fastest of 3 runs to allow for bias due to caching.
Experiment 2: Measuring the Speed of -Exclude
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.
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)
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
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.
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.
Research PowerShell Commands which Use the -Exclude Parameter
# PowerShell script to research parameters
Discover More Members of PowerShell's "-Item" Family
# Members of PowerShell's Item Family
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:
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.