Home » PowerShell » PowerShell Select-String Ignore Case

PowerShell Select-String Ignore Case

In PowerShell, Select-String ignores the case by default. It is case-insensitive. It uses the Pattern parameter to match a species string or regex patterns and returns the results that match the pattern.

The Select-String ignores the case of the text, if the pattern is to search for the word “ShellGeek”, it will match “ShellGeek”, “shellgeek” or “shellGeek” etc… It will match the text regardless of whether the letters are lowercase or uppercase.

In this article, we will discuss how to use the Select-String command to demonstrate the Select-String ignore case and CaseSensitive search by using the -CaseSensitive parameter.

PowerShell Select-String Ignore Case

Use the Select-String in PowerShell to match the text using the pattern string or regex patterns specified. This command ignores the case by default and returns the results regardless of whether the letters in the string are uppercase or lowercase.

# Get the contents of the file
Get-Content -Path D:\Logs\Log1.txt

# Use the Select-String to search for text
Get-Content -Path D:\Logs\Log1.txt | Select-String -Pattern "shellgeek"  

In the above PowerShell script, the Get-Content cmdlet gets the content of the file and passes it to the Select-String cmdlet to search for the text "shellgeek".

The Select-String cmdlet searches for the text regardless of their case, it ignores the case and returns the text that matches.

The output of the above PowerShell script is:

PowerShell Select-String Ignore Case
PowerShell Select-String Ignore Case

PowerShell Select-String Case-Sensitive Search

PowerShell Select-String ignores the case by default, however, to perform a case-sensitive search, use its CaseSensitive parameter.

# Get the content of the file
Get-Content -Path D:\Logs\Log1.txt

# Use Select-String -CaseSensitive to search a text
Get-Content -Path D:\Logs\Log1.txt | Select-String -Pattern "shellgeek" -CaseSensitive

In the above PowerShell script, the Select-String uses the CaseSensitive parameter to search for the specified string and returns the results that exactly match the string.

The output of the above PowerShell script is:

PS C:\> Get-Content -Path D:\Logs\Log1.txt                                                                                  ShellGeek Tutorials
shellgeek tutorials
Shellgeek Tutorials
shellgeek Tutorials



PS C:\> Get-Content -Path D:\Logs\Log1.txt | Select-String -Pattern "shellgeek" -CaseSensitive                              
shellgeek tutorials
shellgeek Tutorials


PS C:\>   

Conclusion

I hope the above article on how to use PowerShell Select-String Ignore case and CaseSensitive search in the file is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.