PowerTip: Find How Many Scripts Use Write-Host


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.

Comments (1)

  1. s.mcknight says:

    …I recently saw the video’s with Jeffery Snover…and wonder why not use Write-Host as I wish to create a Function Prompt that is both individual and colored…and the module I have created does just that…however it requires the use of the Write-Host
    comandlet…any ideas…and is there other issues that make the use of WriteHost…problematic…?

Skip to main content