PowerShell Count Character in String variable

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  
 
PowerShell Character Count  -

PowerShell Character Count –

share with friends

Share to Google Buzz
Share to Google Plus
Share to LiveJournal
Share to Odnoklassniki

6 Responses to PowerShell Count Character in String variable