Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get-ChildItem -Filter Array

Situation:

  1. Get-ChildItem $Path -Filter *.dll works for me

  2. This works:

    $Path = "$env:windir\system32\*"
    $GuyArray = @("*.dll", "*.exe")
    
    Get-ChildItem $Path -Include $GuyArray
    
  3. But I cannot get this working:

    $Path = "$env:windir\system32\*"
    $GuyArray = @("*.dll", "*.exe")
    
    Get-ChildItem $Path -Filter $GuyArray
    

Error message:

Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'. Specified method is not supported.

Questions:

  1. Does this mean that -Include supports multiple values, but -Filter only allows one value?
  2. If the above explaination is correct, is there a way I could have discovered this from Get-Help gci?
like image 957
Guy Thomas Avatar asked Oct 12 '12 12:10

Guy Thomas


3 Answers

Does this mean that -Include supports multiple values, but -Filter only allows one value?

Yes.

If the above explaination is correct, is there a way I could have discovered this from Get-Help gci?

Yes, but you do not get much information by Get-Help gci -Parameter Filter. But you still can see it is a string, not an array. As for the details, Filter is a provider-specific filter. Get-Help gci cannot tell you anything about implementation in a particular provider. In theory, Get-Help FileSystem (help about this provider) should have explained this.

P.S. Also note that this filter rather uses CMD wildcard rules than PowerShell wilcard rules.

like image 51
Roman Kuzmin Avatar answered Oct 11 '22 01:10

Roman Kuzmin


Using Get-Help:

> get-help Get-ChildItem

NAME
    Get-ChildItem

SYNTAX
    Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [-Exclude <string[]>] [-Force] [-Include <string[]>] [-Name] [-Recurse] [-UseTransaction] [<CommonParameters>]

The SYNTAX section includes the parameter types, from which you can see that Filter is a string, and Path an array.

like image 35
Paolo Tedesco Avatar answered Oct 11 '22 03:10

Paolo Tedesco


Question 1:

Yes. -Filter accepts only [string] as input. -Include accepts [String[]].

Question 2:

Get-help get-childitem -parameter filter gives

-Filter <string>
...explanation...

Get-help get-childitem -parameter include gives

-Include <string[]>
...explanation...
like image 38
CB. Avatar answered Oct 11 '22 02:10

CB.