要在PowerShell中搜索文件中的内容,首先需要使用Get-Content命令从文件中获取内容,然后需要添加Select-Stringpipeline命令。在下面的示例中,我们需要搜索包含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