简体   繁体   中英

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?

Try like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM