PowerShell Script to Count Words in a File
Use
(Get-Content filename.txt -Raw) -split '\s+' | Where-Object { $_ -ne '' } | Measure-Object | Select-Object -ExpandProperty Count to count words in a file in PowerShell.Examples
InputHello world
Output2
InputPowerShell is great for automation
Output5
Input
Output0
How to Think About It
To count words in a file, first read the entire file content as one string, then split it by spaces or whitespace to separate words, filter out empty strings, and finally count how many pieces result from the split.
Algorithm
1
Read the whole file content as a single string.2
Split the string into words using whitespace as separator.3
Filter out empty strings from the split result.4
Count the number of words obtained.5
Return the count.Code
powershell
$filePath = "sample.txt" $content = Get-Content $filePath -Raw $words = $content -split '\s+' $count = ($words | Where-Object { $_ -ne '' }).Count Write-Output "Word count: $count"
Output
Word count: 5
Dry Run
Let's trace counting words in a file with content 'PowerShell is great for automation'.
1
Read file content
'PowerShell is great for automation'
2
Split content by whitespace
['PowerShell', 'is', 'great', 'for', 'automation']
3
Count words
5
| Step | Action | Value |
|---|---|---|
| 1 | Read content | PowerShell is great for automation |
| 2 | Split words | PowerShell, is, great, for, automation |
| 3 | Count words | 5 |
Why This Works
Step 1: Read entire file as one string
Using Get-Content -Raw reads the whole file at once, preserving spaces and line breaks.
Step 2: Split string into words
Splitting by \s+ breaks the string at any whitespace, separating words.
Step 3: Count non-empty words
Filtering out empty strings ensures only real words are counted, then Count gives the total.
Alternative Approaches
Using Select-String and regex
powershell
$content = Get-Content sample.txt -Raw $matches = [regex]::Matches($content, '\w+') $count = $matches.Count Write-Output "Word count: $count"
This uses regex to find word characters, which can be more precise but slightly slower.
Reading file line by line and counting words
powershell
$count = 0 Get-Content sample.txt | ForEach-Object { $count += ($_ -split '\s+').Where({$_ -ne ''}).Count } Write-Output "Word count: $count"
Processes file line by line, useful for very large files to save memory.
Complexity: O(n) time, O(n) space
Time Complexity
The script reads the entire file once and splits the content once, so time grows linearly with file size.
Space Complexity
Storing the whole file content and the split words requires memory proportional to file size.
Which Approach is Fastest?
Reading the whole file at once and splitting is fastest for small to medium files; line-by-line is better for large files.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Whole file split | O(n) | O(n) | Small to medium files |
| Regex match | O(n) | O(n) | Precise word matching |
| Line-by-line count | O(n) | O(1) | Large files with low memory |
Use
-Raw with Get-Content to read the whole file as one string for easier word splitting.Not filtering out empty strings after splitting can cause incorrect word counts.