你可以簡單地把它:
$yoursearch = "PowerShell is cool!"
get-content "c:\temp\*.*" -TotalCount 1 | where { $_ -ilike "*$yoursearch*"} | select PSPath, @{N="Founded";E={$_}}
或用於非純粹主義者很短的版本:
gc "c:\temp\*.*" -To 1 | ? { $_ -ilike "*$yoursearch*"} | select PSPath, {$_}
如果你想導出結果:
$yoursearch = "PowerShell is cool!"
get-content "c:\temp\*.*" -TotalCount 1 | where { $_ -ilike "*$yoursearch*"} | select PSPath, @{N="Founded";E={$_}} |
export-csv "c:\temp\yourresult.csv" -notype
如果你想要一個對於文件的輸入濾波器更好:
Get-ChildItem "c:\temp" -File |
Where {$firstrow= (Get-Content $_.FullName -TotalCount 1); $firstrow -ilike "*$yoursearch*"} |
Select fullName, @{N="Founded";E={$firstrow}} |
Export-Csv "c:\temp\yourresult.csv" -notype
或短版非純粹主義者:
gci "c:\temp" -File | ? {$r= (gc $_.FullName -TotalCount 1); $r -ilike "*$yoursearch*"} |
Select f*, @{N="Founded";E={$r}} |
epcsv "c:\temp\yourresult.csv" -notype
注:-file選項僅在PowerShell中V5(或+),否則使用psiscontainer propertie到其中存在指令
注2:可以使用選擇串的選項-list,SEACH所有文件,但停止時1行成立
$yoursearch = "PowerShell where are you"
Select-String -Path "c:\temp\*.*" -Pattern $yoursearch -list | select Path, Line | export-csv "C:\temp\result.csv" -NoTypeInformation
只是試過它對我有什麼,也許應該指定我想要的模式是在多個文件。無論如何,只是測試兩個和沒有寫入,他們給我2分13秒和4分42秒 – Adlis
流媒體閱讀器上的4分鐘?你可以減少選擇文件集嗎? – Matt
另外我想多個文件正在返回。你如何衡量執行時間? – Matt