How to Get Word Count and Character Count in Powershell

To get word count and character count in PowerShell, you can use Measure-Object cmdlet, i.e.:

"dfsdf fdsf dsf df" | Measure-Object -Word

prints

Lines Words Characters Property
----- ----- ---------- --------
          4

and

"dfsdf fdsf dsf df" | Measure-Object -Word

prints

Lines Words Characters Property
----- ----- ---------- --------
                    17

You can also add the following convenience functions to your profile:

function mo {
    param(
        [Parameter(ValueFromRemainingArguments=$true)]
        [string[]] $inputArgs
    )
    $joinedArgs = $inputArgs -join ' '
    $joinedArgs | Measure-Object -Word -Character | Select-Object Words, Characters
}

function cmo {
    $text = Get-Clipboard
    $text | Measure-Object -Word -Character | Select-Object Words, Characters    
}

where mo takes all of the arguments, treats them as text, and prints both word and character count, and cmo gets text from clipboard and calculates the same stats as above.


To contact me, send an email anytime or leave a comment below.