PowerShell – Do Not use Write-Host!!!!!

Problem Statement: People often use Write-Host in their script, but it is considered harmful. Use other options instead.

Solution: Write-Host is always the wrong thing to do while writing or reviewing the Powershell scripts because it interferes with automation. So, rather than using Write-Host try using any one of the following commandlets as per your requirement:

♦ Write-Debug
  • The Write-Debug cmdlet writes debug messages to the console from a script or command.
  • The text foreground color will be CYAN.
  • Used when you want to run your Powershell script in debug mode. A pop window will display after write-Debug command is hit and script want move forward until you press any of the option given in the popup window.


  • Write-Debug command in your script, will invoke only if you have specified -Debug while calling the script.
  • Example:
    PS C:\>Write-Debug "Cannot open file." -debug
♦ Write-Error
  • Write an object to the error pipeline. Write-Error messages along with other information such as an id, object data, and suggested actions.
  • Specifies the category of the error. The default value is NotSpecified.
  • Example:
Write-Error -Message "Error: Too many input values." -Category InvalidArgument
 ♦ Write-Information
  • Write-Information lets you add an informational message to the stream and specifies how Windows PowerShell handles information stream data for a command.
  • The $InformationPreference preference variable value determines whether the message you provide to Write-Information is displayed at the expected point in a script’s operation.
  • Because the default value of this variable is SilentlyContinue, by default, informational messages are not shown.
  • Example:
function Test-Info
         Get-Process P*
         Write-Information "Here you go"
Test-Info 6> Info.txt
♦ Write-Output
  • The Write-Output cmdlet sends the specified object down the pipeline to the next command. If the command is the last command in the pipeline, the object is displayed in the console.
  • This cmdlet is typically used in scripts to display strings and other objects on the console. However, because the default behavior is to display the objects at the end of a pipeline, it is generally not necessary to use the cmdlet. For example, “get-process | write-output” is equivalent to “get-process”.
  • Example:
write-output "test output" | get-member
♦ Write-Progress
  • The Write-Progress cmdlet displays a progress bar in a Windows PowerShell command window that depicts the status of a running command or script.
  • You can select the indicators that the bar reflects and the text that appears above and below the progress bar.
  • Example:
for ($i = 1; $i -le 100; $i++ )
{write-progress -activity "Search in Progress" -status "$i% Complete:" -percentcomplete $i;}
♦ Write-Verbose
  • The Write-Verbose cmdlet writes text to the verbose message stream in Windows PowerShell. Typically, the verbose message stream is used to deliver information about command processing that is used for debugging a command.
  • By default, the verbose message stream is not displayed, but you can display it by changing the value of the $VerbosePreference variable or using the Verbose common parameter in any command.
  • Example:
$VerbosePreference = "Continue"
PS C:\>Write-Verbose "Copying file $filename"
♦ Write-Warning
  • The Write-Warning cmdlet writes a warning message to the Windows PowerShell host.
  • The response to the warning depends on the value of the user’s $WarningPreference variable and the use of the WarningAction common parameter.
  • Example:
write-warning "This is only a test warning."

Leave a Reply

Your email address will not be published. Required fields are marked *