Unix To PowerShell - Wc
PowerShell is definitely gaining momentum in the windows scripting world but I still hear folks wanting to rely on unix based tools to get their job done. In this series of posts I’m going to look at converting some of the more popular Unix based tools to PowerShell.
wc
The Unix “wc” (word count) command will print the character, word, and newline counts for each file specified and a total line if more than one file is specified. This command is useful for quickly scanning a directory for small and large files or to quickly look at a file and determine it’s relative size. The Get-Content Cmdlet will return the number of characters in the full but not the number of lines and words. The following script will emulate the behavior of the Unix “wc” command with a few changes in the way parameters are supplied.
- DaveRob_137318NimbostratusI'm not seeing any script or anything to click on to get the script?
- Thanks Doug. There are a couple of issues with GC and Measure-Object in that they don't work well with multiple files while also itemizing the individual files. Also, blank lines are not counted in the line count for Measure-Object and newlines are not counted in the character count as they are in the unix wc command. I've modified the above to use Measure-Object but on a line by line basis. It did speed things up a bit.