PowerTip: Find How Many Scripts Use Write-Host

Doctor Scripto

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.

0 comments

Discussion is closed.

Feedback usabilla icon