How does the Get-ChildItem -Exclude Parameter parameter work?

How does the Get-ChildItem -Exclude parameter work? What are the rules followed?

Get-Help for Get-ChildItem is not detailed:

Omit the specified elements. The value of this parameter qualifies the path parameter. Enter an element or path template, for example "* .txt". Wildcards are allowed.

And on Stackoverflow and elsewhere, the general consensus seems too complicated to use, and we all just need to output Get-ChildItem output to Where-Object.

While I'm ready to use the Where-Object, I'm curious about the -Exclude rules.

For example, I have a folder with the following subfolders:

HsacFixtures HsacFixturesBuild RestFixture RestFixtureBuild 

If I run the following command:

 Get-ChildItem $rootFolderPath -Exclude HsacFixturesBuild -Directory 

it returns the expected results:

 HsacFixtures RestFixture RestFixtureBuild 

However, if I add the -Recurse parameter:

 Get-ChildItem $rootFolderPath -Exclude HsacFixturesBuild -Directory -Recurse 

It then returns the subfolders in the HsacFixturesBuild folder.

I also tried HsacFixturesBuild\ and HsacFixturesBuild\* , which have the same results.

So, -Exclude applies only to immediate children, not grand-children or deeper subfolders?

+5
source share
2 answers

Exclude omits child objects based on Name ,

For files, gets the file name. For directories, gets the name of the last directory in the hierarchy if a hierarchy exists. Otherwise, the Name property gets the name of the directory.

not FullName ,

which gets the full path to the directory or file

Thus, although the object is a grandson in a recursive call, exclude only considers the Name object, not the FullName , so the exception does not affect omission if the children do not have a common name substring that is part of the exclude parameter

Get-ChildItem Source

Example 3: Get all children using include and exclusion

This command lists the .txt files in the Logs subdirectory , with the exception of those whose names begin with the letter A. It uses the character (*) template to specify the contents of the Logs subdirectory, not the directory container. Because the command does not include the Recurse parameter, the command does not include the contents of the directory automatically; you need to specify it.

Windows PowerShell

 PS C:\> Get-ChildItem –Path "C:\Windows\Logs\*" -Include "*.txt" -Exclude "A*" 
+6
source

For Get-ChildItem the -exclude works with name objects

The following example is better understood: Consider the following folder structure

enter image description here

First we use the -exclude option without recurse. enter image description here

As we can see in the above image, based on the name , folder objects, it was excluded

Now we will add the recurse parameter to the above statement as follows enter image description here

Now we see that the subfolders of folder are still present, since the exception was applied to name objects

Hope these are HElps.

+3
source

Source: https://habr.com/ru/post/1259127/


All Articles