#requires -version 2.0 <# ----------------------------------------------------------------------------- Script: Get-ContentWords.ps1 Version: 0.1 Author: Jeffery Hicks http://jdhitsolutions.com/blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Date: 1/24/2012 Keywords: Comments: Get a list of sorted unique "words" from a text file or script. Good way to catch typos. "Those who forget to script are doomed to repeat their work." **************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** ----------------------------------------------------------------------------- #> Function Get-ContentWords { [cmdletbinding()] Param ( [Parameter(Position=0,Mandatory=$True, HelpMessage="Enter the filename for your text file", ValueFromPipeline=$True)] [string]$Path ) Begin { Set-StrictMode -Version 2.0 Write-Verbose "Starting $($myinvocation.mycommand)" #define a regular expression pattern to detect "words" [regex]$word="\b\S+\b" } Process { if ($path.gettype().Name -eq "FileInfo") { #$Path is a file object Write-Verbose "Getting content from $($Path.Fullname)" $content=Get-Content -Path $path.Fullname } else { #$Path is a string Write-Verbose "Getting content from $path" $content=get-content -Path $Path } #add a little information $stats=$content | Measure-Object -Word Write-Verbose "Found approximately $($stats.words) words" #write sorted unique values $word.Matches($content) | select Value -unique | sort Value } End { Write-Verbose "Ending $($myinvocation.mycommand)" } } #close function