How to search in the file using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To search the content in the file in PowerShell, you need to first get the content from the file using Get-Content command and then you need to add Select-String pipeline command. In the below example, we need to search the lines which contain the Get word.

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

You can use multiple patterns to search for the result.

Example

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

Output

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
raja
Published on 16-Mar-2020 07:51:06
Advertisements