Getting the Number of Lines in a String
All PowerShell Versions Here is a clever trick how to find out how many lines a string (not a string array!) contains: $text = @'...
- Written By: ps1
- Last Updated: April 18, 2025
- 1 minute read
All PowerShell Versions Here is a clever trick how to find out how many lines a string (not a string array!) contains: $text = @'...
Here is a clever trick how to find out how many lines a string (not a string array!) contains:
$text = @' This is some sample text Let's find out the number of lines. '@ $text.Length - $text.Replace("`n",'').Length + 1
Technically, the example uses a here-string to create the multi-line string, but this is just an example. It works for all kinds of strings, regardless of origin.
Contact our sales team to get a personalized demo of our database management software for SQL Server!