2

I have following folders inside the "clients" directory

Arizona
California
Connecticut
Default
Delaware
Florida
Hawaii
Idaho
Iowa
Maine

I have an array and it has following items:

Write-Host "Printing the array " 
$ExcludeArray

Output----
"Arizona",
"California"  

I'm trying to list the folders inside C:\Users\santhu\Desktop\Clients and exclude the folders in my $ExcludeArray array using following command.

Get-Childitem C:\Users\santhu\Desktop\Clients -Force -Exclude $ExcludeArray

It seems the exclude is not working as expected. I believe the reason could be the items in $ExcludeArray are not in same line. Can someone please suggest me if there is any way to convert them to same line and filter the folders? FYI: the $ExcludeArray items are not static. I cannot write something like $ExcludeArray = "Arizona","California". I'm expecting following output.

Mode                LastWriteTime     Length Name                                                                       
----                -------------     ------ ----                                                                                                                                                                                                       
d----         3/20/2017  12:15 PM            Connecticut                                                                
d----         3/20/2017  12:15 PM            Default                                                                    
d----         3/20/2017  12:15 PM            Delaware                                                                   
d----         3/20/2017  12:15 PM            Florida                                                                    
d----         3/20/2017  12:15 PM            Hawaii                                                                     
d----         3/20/2017  12:15 PM            Idaho                                                                      
d----         3/20/2017  12:15 PM            Iowa                                                                       
d----         3/20/2017  12:15 PM            Maine                                                                      
d----         3/20/2017  12:15 PM            Minnesota 
mklement0
  • 382,024
  • 64
  • 607
  • 775
mahesh
  • 468
  • 2
  • 8
  • 25

3 Answers3

1

Try this:

Get-Childitem C:\Users\santhu\Desktop\Clients\* -Force -Exclude $ExcludeArray

The exclude and include switch is a bit tricky here. It works on the last mentioned path. So wildcard should do your work. Make sure you are not having any double quotes inside the array.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
1
  • We now know that your primary problem was the content of $ExcludeArray, your dynamically constructed array of strings (exclusion patterns), whose elements either mistakenly had embedded " chars. (E.g, "Arizona" instead of just Arizona) or, possibly, $ExcludeArray mistakenly contained a single, multi-line string rather than array.

    • To read lines from a file into the elements of a string array, all that is needed is $ExcludeArray = Get-Content file

    • If the file's lines contain strings enclosed in " chars. possibly terminated with , (e.g., "Arizona", or "California", you need to trim those characters:
      $ExcludeArray = (Get-Content File) -replace '^"(.*)",?', '$1'

  • With a correctly constructed array your original command does work, but only if none of the patterns in $ExcludeArray match the last component of the input path - Client, in this case.

    • In other words: -Exclude and -Include are perhaps unexpectedly compared to the input path's last path component (the file name in case of a file path, the directory name in case of a directory path).

    • In the case of Get-ChildItem, that matching against the parent directory name happens before the names of the child items (the items inside the directory) are matched, and if the parent doesn't match, the child items aren't even looked at, and nothing is output.

    • This answer of mine explains this unintuitive behavior in more detail.

It is therefore better to find a generally robust idiom that solves your problem, where matching against the parent directory's name is avoided:

  • Switch from Get-ChildItem to Get-Item.
  • Append \* to your input path.

Specifically:

Get-Item C:\Users\santhu\Desktop\Clients\* -Force -Exclude $ExcludeArray

By contrast, if you append \* but stick with Get-*Child*Item, as in Ranadip Dutta's answer, your command will behave differently: You'll get the contents (child items) of any child directories that aren't excluded, not the directories themselves.

Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • @mklement0- First of all thanks a lot for the response. I agree. I will try this too. – mahesh Mar 28 '17 at 14:54
  • @mahesh: My pleasure; I felt I needed to post my own answer, because Ranadip's answer mistakenly solves a different problem. – mklement0 Mar 28 '17 at 15:46
0

this code work on my pc

$ExcludeArray="Arizona","California"
Get-ChildItem "C:\Users\santhu\Desktop\Clients" -Exclude $ExcludeArray

or like this

$ExcludeArray="Arizona","California"
Get-ChildItem "C:\Users\santhu\Desktop\Clients" | where Name -notin $ExcludeArray
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • The 1st command _happens_ to work _in this case_, but is not generally robust - consider what happens when you use `Get-ChildItem C:\Windows -Exclude W*` - if one of the exclusion patterns happens to match the _parent_ directory's name, you get no results at all. Aside from using PSv3+ syntax, the 2nd command is a viable alternative, albeit slower than a (robust) `-Exclude`-based solution. – mklement0 Mar 28 '17 at 15:49