#Requires -version 2.0 # ----------------------------------------------------------------------------- # Script: ConvertTo-NumberedText.ps1 # Version: 1.0 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # Date: 3/22/2011 # Keywords: # Comments: # # "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. * # **************************************************************** # ----------------------------------------------------------------------------- $File=Read-Host "Enter the name of the file to convert to numbered output" $Output=Read-Host "Enter the name of the output file" if (Test-Path -Path $file) { #read in the total contents of the file $Content=get-content -Path $file #get the total number of lines $count=$content.Count #convert the count to a string so we can find how many digits. This will #be the padding value $Pad=$count.ToString().Length #initialize a counter $i=1 #pipe the content to ForEach, building a new numbered line and pipe the results to a new #file. The new lines are then piped to Out-File. $content | foreach { #use the -f operaton to construct a string with a padded line number #and the line. "{0} {1}" -f $i.ToString().PadLeft($pad),$_ #increment the counter $i++ } | Out-File -FilePath $Output Write-Host "Open $Output for numbered content." -ForegroundColor Green } #if else { Write-Warning "Can't find or verify $file." }