PowerShell Get-Childitem -Include

PowerShell GCI -Include Parameter

My advice is to avoid the -Include parameter.  If you want to filter the output then use the -Filter parameter!  The problem with -Include is that it doesn't do what you think.  In the case of Get-ChildItem, -Include operates on the path and not on the individual filenames. To overcome this limitation you need a wildcard* or the -Recurse parameter.

Topics for Get-ChildItem -Include

 ♣

Example 1: Let Us Get Some Action with -Include

Scenario: You want an inventory of only the executables in the Windows System32 folder.

# PowerShell GCI -Include example
Get-ChildItem -Path $env:SystemRoot\System32 -Include *e*

Problem 1: You get a few .exe files, but also an assortment of other file types.

Explanation: You struck lucky, the System32 contains 'e'!

Example 2:  The -Include Parameter is a Huge Disappointment

The same scenario: You want a list of exe files in the Windows System32 folder. This time you try *.exe.

Clear-Host
Get-ChildItem -Path $env:SystemRoot\System32 -Include *.exe

Problem 2: No files are returned.  Have you realized why?  There is no .exe. in the fullname of the stream of objects created by Get-ChildItem.

Example 3: Adjustments to Get -Include Working

Solution A: Add a *wildcard after the folder name:

# PowerShell -Include working example
Clear-Host
Get-ChildItem -Path $env:SystemRoot\System32\* -Include *.exe

Note 1: Appending \* to the path makes all the difference.

Solution B: Append -Recurse

Clear-Host
Get-ChildItem -Path $env:SystemRoot\System32 -Include *.exe -Recurse

This gets -Include working, but you may not want to 'Recurse' through all those subfolders.

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

Example 4: Ditch -Include; Prefer -Filter

Solution C: Ditch -Include; and substitute a parameter called -Filter.

Clear-Host
Get-ChildItem -Path $env:SystemRoot\System32 -Filter *.exe

Eureka:  You get a neat list of all the executables in the System32.  No unwanted files from the folders lower down the tree.

Conclusion: Now you know why -Include is my least favorite PowerShell parameter, wherever possible employ the -Filter instead.  I confess that I have deliberately not shown -Include in its best possible light, nevertheless it always disappoints me whenever I give it a try.

Further Research on Get-ChildItem

Get-Help for Get-ChildItem

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

Note 2: This reveals parameters such as -Include and -Filter.

Compare with PowerShell's -Filter parameter »

Summary of Get-ChildItem -Include

I don't rate the -Include parameter.  To date I have not found one use for it.  When I want to refine the output I find that -filter is far superior.

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-2016 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: