如何使用 PowerShell 在檔案中進行搜尋?


若要搜尋 PowerShell 中檔案中的內容,你需要先使用 **Get-Content** 命令從檔案中獲取內容,然後你需要新增 **Select-String** 流水線命令。在下例中,我們需要搜尋包含 Get 單詞的行。

PS C:\WINDOWS\system32> Get-Content D:\Temp\PowerShellaliases.txt | Select-String -Pattern Get
Alias           cat -> Get-Content
Alias           dir -> Get-ChildItem
Alias           gal -> Get-Alias
Alias           gbp -> Get-PSBreakpoint
Alias           gc -> Get-Content
Alias           gcb -> Get-Clipboard                               3.1.0.0    Microsoft.PowerShell.Management
Alias           gci -> Get-ChildItem
Alias           gcm -> Get-Command
Alias           gcs -> Get-PSCallStack
Alias           gdr -> Get-PSDrive
Alias           ghy -> Get-History
Alias           gi -> Get-Item
Alias           gin -> Get-ComputerInfo                            3.1.0.0    Microsoft.PowerShell.Management
Alias           gjb -> Get-Job
Alias           gl -> Get-Location
Alias           gm -> Get-Member
Alias           gmo -> Get-Module
Alias           gp -> Get-ItemProperty
Alias           gps -> Get-Process
Alias           gpv -> Get-ItemPropertyValue

你可以使用多個模式搜尋結果。

示例

Get-Content D:\Temp\PowerShellaliases.txt | Select-String -Pattern Get,Set

輸出

Get-Content D:\Temp\PowerShellaliases.txt | Select-String -Pattern Get,Set
Alias           gsnp -> Get-PSSnapin
Alias           gsv -> Get-Service
Alias           gtz -> Get-TimeZone
Alias           gu -> Get-Unique
Alias           gv -> Get-Variable
Alias           gwmi -> Get-WmiObject
Alias           h -> Get-History
Alias           history -> Get-History
Alias           ls -> Get-ChildItem
Alias           ps -> Get-Process
Alias           pwd -> Get-Location
Alias           sal -> Set-Alias
Alias           sbp -> Set-PSBreakpoint
Alias           sc -> Set-Content
Alias           scb -> Set-Clipboard
Alias           set -> Set-Variable
Alias           si -> Set-Item
Alias           sl -> Set-Location
Alias           sp -> Set-ItemProperty
Alias           stz -> Set-TimeZone

更新於: 16-3-2020

超過 1.3 萬次瀏覽

開啟你的 職業

完成課程,獲得認證

開始
廣告