October 4th, 2014

PowerTip: Find How Many Scripts Use Write-Host

Doctor Scripto
Scripter

Summary: Use Windows PowerShell to find how many scripts use the Write-Host cmdlet.

Hey, Scripting Guy! Question I am trying to improve how I write Windows PowerShell scripts, and I want to reduce the number of times I use 
           Write-Host. How can I find how many times I used this cmdlet in my scripts?

Hey, Scripting Guy! Answer Use the Get-ChildItem cmdlet to retrieve all .ps1 files from your script directory. Then pipe the FileInfo objects
           to the Select-String cmdlet and look for the pattern Write-Host. Use the Quiet switch and pipe the results to
           the Group-Object cmdlet, for example:

gci E:\Data\PSExtras\ -Filter *.ps1 | Select-String -Pattern "write-host" -Quiet | group

Note  gci is an alias for Get-ChildItem, and group is an alias for Group-Object.

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.