Posted: . At: 9:46 AM. This was 7 months ago. Post ID: 18576
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to use the equivalent of grep in Powershell.


Using the grep equivalent in Powershell is very easy. This is simple text filtering. Below is a simple example.

PS C:\Users\Intel i5\Documents> ([regex]::Matches((Invoke-RestMethod "https://www.dailytelegraph.com.au/news/nsw"), '(?<=data-tgev-container="feed-1">).*?(?=</a>)') | ForEach-Object { $_.Value }) | sls gathering
 
‘Can’t stop people gathering’, cops admit as tension rises

Here is another simple example.

PS C:\Users\Intel i5\Documents> ps | sls edge
 
System.Diagnostics.Process (ArmouryWebBrowserEdge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedge)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)
System.Diagnostics.Process (msedgewebview2)

Another way to handle this is to use object-oriented filtering.

PS C:\Users\Intel i5\Documents> Get-NetAdapter | where Name -like Ethernet
 
Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
----                      --------------------                    ------- ------       ----------             ---------
Ethernet                  Intel(R) Ethernet Connection (14) I2...      20 Up           FC-34-97-A5-BC-7E         1 Gbps

This will show only the Ethernet line that is found in the Name column.

Another way, this will only show the Name column in the output.

PS C:\Users\Intel i5\Documents\sysinfo-master> Get-ChildItem | Select-Object Name
 
Name
----
.vscode
obj
src
LICENSE
makefile
README.md
sysinfo
system-info

And this is how to show a few columns.

PS C:\Users\Intel i5\Documents\sysinfo-master> Get-ChildItem | Select-Object Name,Length,Mode
 
Name        Length Mode
----        ------ ----
.vscode            d-----
obj                d-----
src                d-----
LICENSE     18015  -a----
makefile    714    -a----
README.md   351    -a----
sysinfo     22048  -a----
system-info 12921  -a----

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.