April 25th, 2006

Exclude, Include, Filter Parameters – How to make sense of these

PowerShell Team
PowerShell Team

So how come we have three ways to filter a path in most of the core cmdlets such as in the get-item cmdlet.  It is important to understand that exclude, include, and filter offer different levels of filtering.

 

So let’s give a few examples of how get-item cmdlet works with these parameters individually and then in conjunction.

 

MSH C:\monad> get-item file*

    Directory: Microsoft.Management.Automation.Core\FileSystem::C:\monad

 

Mode

LastWriteTime

Length

Name

-a—         

3/3/2006   4:00 PM

600

fileA

-a—         

3/3/2006   5:00 PM

700

fileB

 

We have two items and we are only interested in retrieving fileA. There are two ways to achieve this.

 

One way is to use the exclude parameter which removes (or excludes) item(s) that are not of interest.

 

MSH C:\monad> get-item file* -exclude fileB

    Directory: Microsoft.Management.Automation.Core\FileSystem::C:\monad

 

Mode

LastWriteTime

Length

Name

-a—         

3/3/2006   4:00 PM

600

fileA

 

The other approach is to use the include parameter which only allows item(s) of interest to be displayed.

 

MSH C:\monad> get-item file* -include fileA

    Directory: Microsoft.Management.Automation.Core\FileSystem::C:\monad

 

Mode

LastWriteTime

Length

Name

-a—         

3/3/2006   4:00 PM

600

fileA

 

The above two scenarios are quite straightforward, but say you were trying to search for a file in the Windows directory that started with the letter ‘a’ and did not end with the letter ‘z’.

 

The below command shows how easily that can be done with the use of exclude and include parameters.

 

MSH C:\monad> get-item C:\Windows\* -include a* -exclude *z

 

The filter parameter gives the ability for the provider to perform additional filtering based upon some provider-specific string.  This is provider specific and hence not all providers do something special given a filter parameter input.  In the File System Provider case, the filter parameter performs an inclusive filter similar to the include parameter but it is implemented natively. Hence, you would notice significant performance boost when trying to perform searches within a very large set of files using the filter parameter versus the include parameter.  The both achieve the same result but filter parameter can be a time saver during instances of large loads.

One thing I forgot to mention is that exclude, include parameters work on the leaf items and not on the absolute path. What does this mean?

In our previous example, where we were trying to extract fileA from a list of files, we cannot specify the absolute path

MSH C:\monad> get-item C:\monad\file* -include C:\monad\fileA  //will not work

 

However, just specifying the leaf item will work as expected.

MSH C:\monad> get-item C:\monad\file* -include fileA

    Directory: Microsoft.Management.Automation.Core\FileSystem::C:\monad

Mode

LastWriteTime

Length

Name

-a—         

3/3/2006   4:00 PM

600

fileA

 

 

-Satish

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Category
PowerShell

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.