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


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

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


Comments (2)

  1. Sam says:

    IMHO the difference between filter and include becomes more apparent with get-childitem, especially compared to an old-fashioned ‘dir’:

    Once upon a time I searched my HDD for a special file using something like

    dir c:hosts /s

    So my first naive attempt to do this in Monad has been:

    get-childitem c:hosts -r

    This does not work, since there is nothing named host on the root of c:

    What I need to do is

    get-childitem c:* -r -i hosts