Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / Grep in PowerShell Using the Select-String Cmdlet

April 5, 2022 PowerShellWindows 10Windows Server 2019

Grep in PowerShell Using the Select-String Cmdlet

In Linux/Unix, the grep command is used to find a text (string) or errors in a log file. Grep allows you to find/select any data in the output of another command:

command | grep search

In PowerShell, you can use the Select-String cmdlet to find a text string in a file.

For example, the following command displays all lines containing ERROR in a text file or stdout:

Select-String -Path c:\tmp\makeapp_sxtracesxs.txt -Pattern "ERROR"

Select-String or grep in Powershell

The command has shown the number of lines that contain the text you are looking for and their values.

If you want to search for a string in all TXT files in a specific directory, run the command below:

Select-String -Path c:\tmp\*.txt -Pattern "ERROR"

You can use this command if you want to search through all files in a folder. For example, this command can be useful for searching transport (SMTP) and message tracking logs on Exchange Server.

Using the Exclude and Include options, you can include or exclude certain files for search. The following command will search through all TXT and LOG files that do not contain copy in their names:
$path = "c:\tmp\*"
Select-String -Path $path -Pattern "ERROR" -Include "*.txt","*.log" -Exclude "*copy*"

The previous example searches for a text in the specified directory only. To search files in the nested directories recursively, use the Get-ChildItem cmdlet:

Get-ChildItem -Path 'c:\tmp\' -Recurse -include "*.mp3","*.avi" -ErrorAction SilentlyContinue | Select-String -SimpleMatch "ERROR","WARNING"

0 comment
0
Facebook Twitter Google + Pinterest
previous post
Can’t Access Shared Folders or Map Network Drives from Windows 10 and 11
next post
VMWare ESXi: Errno 28 — No space left on device

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

November 7, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Fix: Remote Desktop Licensing Mode is not Configured
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • How to Delete Old User Profiles in Windows
  • Get-ADUser: Find Active Directory User Info with PowerShell
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top