This is the easy way — $var.length
$characters = "abcdefg" #store the number count in $x $x= $characters.length write-host $characters.length $x #result PS C:\scripts\powershell> 7 7
Below is the hard way
$characters = "abcdefg" $charCount = ($characters.ToCharArray() | Where-Object {$_} | Measure-Object).Count write-host $charCount PS C:\scripts\powershell 7
6 Responses to PowerShell Count Character in String variable