ozOracle
ozOracle

Reputation: 57

Powershell: how to pass filter value to gci

I've got files that matches M*,S* in a folder:

PS E:\Uploader> dir M*,S*
    Directory: E:\Uploader
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-----         6/05/2013  10:43 AM    5046659 M_1813O.zip
-a---        26/04/2013   4:51 PM        233 SurveyUploader.config
-a---         8/05/2013   5:15 PM      11788 SurveyUploader.log
-a---         8/05/2013   5:15 PM       6078 SurveyUploader.ps1
-----         6/05/2013  10:43 AM    3473113 S_1813O.zip

I simply want to pass this filter to gci in a variable, but it doesn't work:

PS E:\Uploader> $k="M*,S*"
PS E:\Uploader> gci *.* -include $k | where {!$_.PsIsContainer}
PS E:\Uploader> gci *.* -include @($k) | where {!$_.PsIsContainer}
<<no files returned!>>

If I don't save the filter in a variable, it works fine:

PS E:\Uploader> gci *.* -include S*,M* | where {!$_.PsIsContainer}
    Directory: E:\temp\SurveyLoadingAutomation\Uploader
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-----         6/05/2013  10:43 AM    5046659 M_1813O.zip
-a---        26/04/2013   4:51 PM        233 SurveyUploader.config
-a---         8/05/2013   5:15 PM      11788 SurveyUploader.log
-a---         8/05/2013   5:15 PM       6078 SurveyUploader.ps1
-----         6/05/2013  10:43 AM    3473113 S_1813O.zip

But I need to pass the filter in a variable in a script.

Any idea?

Upvotes: 2

Views: 971

Answers (1)

CB.
CB.

Reputation: 60958

Try like this:

$k=  @("M*","S*")

the -include parameter accept [string[]] (an array of string type )

Upvotes: 5

Related Questions