Counting the number of occurrences of a string or file in PowerShell

I always forget how to do this, and Aleksandar Nikolić posted a really beautiful answer on

For a file:

-split (Get-Content .\test.txt | Out-String) | Where-Object { $_ -eq "test" } | Measure-Object | Select-Object -exp count

For a string:

$html.content -split "FeedbackItemSummaryModulePanel" | Measure-Object | Select-Object -Exp Count

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in PowerShell