Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • 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 2012
    • 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

February 1, 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

Using Previous Command History in PowerShell Console

January 31, 2023

How to Install the PowerShell Active Directory Module...

January 31, 2023

Enable Internet Explorer (IE) Compatibility Mode in Microsoft...

January 27, 2023

Finding Duplicate E-mail (SMTP) Addresses in Exchange

January 27, 2023

How to Disable or Uninstall Internet Explorer (IE)...

January 26, 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

  • Using Previous Command History in PowerShell Console

    January 31, 2023
  • How to Install the PowerShell Active Directory Module and Manage AD?

    January 31, 2023
  • Finding Duplicate E-mail (SMTP) Addresses in Exchange

    January 27, 2023
  • How to Delete Old User Profiles in Windows?

    January 25, 2023
  • How to Install Free VMware Hypervisor (ESXi)?

    January 24, 2023
  • How to Enable TLS 1.2 on Windows?

    January 18, 2023
  • Allow or Prevent Non-Admin Users from Reboot/Shutdown Windows

    January 17, 2023
  • Fix: Can’t Extend Volume in Windows

    January 12, 2023
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11

    January 11, 2023
  • Adding Trusted Root Certificates on Linux

    January 9, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Configuring Port Forwarding in Windows
  • Installing RSAT Administration Tools on Windows 10 and 11
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11?
  • Adding Drivers into VMWare ESXi Installation Image
Footer Logo

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


Back To Top