{"id":712,"date":"2015-11-20T12:52:02","date_gmt":"2015-11-20T07:22:02","guid":{"rendered":"http:\/\/pramodsingla.com\/?p=712"},"modified":"2015-11-20T12:52:02","modified_gmt":"2015-11-20T07:22:02","slug":"powershell-do-not-use-write-host","status":"publish","type":"post","link":"https:\/\/pramodsingla.com\/?p=712","title":{"rendered":"PowerShell &#8211; Do Not use Write-Host!!!!!"},"content":{"rendered":"<p style=\"text-align:justify;\"><span style=\"color:#ff0000;\"><strong>Problem Statement:\u00a0<\/strong><\/span>People often use Write-Host in their script, but it is considered <a href=\"http:\/\/www.jsnover.com\/blog\/2013\/12\/07\/write-host-considered-harmful\/\" target=\"_blank\" rel=\"noopener\">harmful<\/a>. Use other options instead.<\/p>\n<p style=\"text-align:justify;\"><span style=\"color:#008000;\"><strong>Solution:\u00a0<\/strong><\/span>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:<\/p>\n<h5 class=\"toclevel2 \" style=\"text-align:justify;\"><span style=\"color:#993300;\"><strong>\u2666\u00a0Write-Debug<\/strong><\/span><\/h5>\n<ul style=\"text-align:justify;\">\n<li class=\"toclevel2 \">The Write-Debug cmdlet writes debug messages to the console from a script or command.<\/li>\n<li class=\"toclevel2 \">The text foreground color will be <strong><span style=\"color:#00ffff;\">CYAN<span style=\"color:#000000;\">.<\/span><\/span><\/strong><\/li>\n<li><em><strong>Used when you want to run your Powershell script in debug mode.\u00a0<\/strong><\/em>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.<\/li>\n<\/ul>\n<p style=\"text-align:justify;\"><img decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-714 aligncenter\" src=\"https:\/\/pramodsingla.files.wordpress.com\/2015\/11\/write-debug.png?w=300\" alt=\"Write-Debug\" width=\"300\" height=\"95\" srcset=\"https:\/\/pramodsingla.com\/wp-content\/uploads\/2015\/11\/write-debug.png 356w, https:\/\/pramodsingla.com\/wp-content\/uploads\/2015\/11\/write-debug-300x95.png 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<ul style=\"text-align:justify;\">\n<li><strong>Write-Debug<\/strong> command in your script, will invoke only if you have specified <strong>-Debug<\/strong> while calling\u00a0the script.<\/li>\n<li><strong>Example:<\/strong>\n<pre>PS C:\\&gt;Write-Debug \"Cannot open file.\" -debug<\/pre>\n<\/li>\n<\/ul>\n<h5 class=\"toclevel2 \" style=\"text-align:justify;\"><span style=\"color:#993300;\"><strong>\u2666\u00a0Write-Error<\/strong><\/span><\/h5>\n<ul style=\"text-align:justify;\">\n<li class=\"toclevel2 \" style=\"text-align:justify;\">Write an object to the error pipeline. Write-Error messages along with other information such as an id, object data, and suggested actions.<\/li>\n<li class=\"toclevel2 \" style=\"text-align:justify;\">Specifies the category of the error. The default value is <strong>NotSpecified.<\/strong><\/li>\n<li class=\"toclevel2 \" style=\"text-align:justify;\"><strong>Example:<\/strong><\/li>\n<\/ul>\n<pre style=\"padding-left:60px;\">Write-Error -Message \"Error: Too many input values.\" -Category InvalidArgument<\/pre>\n<h5 class=\"toclevel2 \" style=\"text-align:justify;\"><span style=\"color:#993300;\">\u00a0\u2666\u00a0<strong>Write-Information<\/strong><\/span><\/h5>\n<ul style=\"text-align:justify;\">\n<li class=\"toclevel2 \" style=\"text-align:justify;\">Write-Information lets you add an informational message to the stream and specifies how Windows PowerShell handles information stream data for a command.<\/li>\n<li class=\"toclevel2 \" style=\"text-align:justify;\">The <strong>$InformationPreference preference variable<\/strong> value determines whether the message you provide to Write-Information is displayed at the expected point in a script\u2019s operation.<\/li>\n<li class=\"toclevel2 \" style=\"text-align:justify;\">Because the default value of this variable is <strong>SilentlyContinue<\/strong>, by default, informational messages are not shown.<\/li>\n<li class=\"toclevel2 \" style=\"text-align:justify;\"><strong>Example:<\/strong><\/li>\n<\/ul>\n<pre style=\"padding-left:60px;\">function Test-Info\n       { \n         Get-Process P*\n         Write-Information \"Here you go\"\n       }\nTest-Info 6&gt; Info.txt<\/pre>\n<h5 class=\"toclevel2 current \" style=\"text-align:justify;\"><span style=\"color:#993300;\"><strong>\u2666\u00a0Write-Output<\/strong><\/span><\/h5>\n<ul style=\"text-align:justify;\">\n<li class=\"toclevel2 current \" style=\"text-align:justify;\">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.<\/li>\n<li class=\"toclevel2 current \" style=\"text-align:justify;\">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, &#8220;get-process | write-output&#8221; is equivalent to &#8220;get-process&#8221;.<\/li>\n<li class=\"toclevel2 current \" style=\"text-align:justify;\"><strong>Example:<\/strong><\/li>\n<\/ul>\n<pre style=\"padding-left:60px;\">write-output \"test output\" | get-member<\/pre>\n<h5 class=\"toclevel2 \" style=\"text-align:justify;\"><span style=\"color:#993300;\"><strong>\u2666\u00a0Write-Progress<\/strong><\/span><\/h5>\n<ul style=\"text-align:justify;\">\n<li class=\"toclevel2 \" style=\"text-align:justify;\">The Write-Progress cmdlet displays a progress bar in a Windows PowerShell command window that depicts the status of a running command or script.<\/li>\n<li class=\"toclevel2 \" style=\"text-align:justify;\">You can select the indicators that the bar reflects and the text that appears above and below the progress bar.<\/li>\n<li class=\"toclevel2 \" style=\"text-align:justify;\"><strong>Example:<\/strong><\/li>\n<\/ul>\n<pre style=\"padding-left:60px;\">for ($i = 1; $i -le 100; $i++ )\n{write-progress -activity \"Search in Progress\" -status \"$i% Complete:\" -percentcomplete $i;}<\/pre>\n<h5 class=\"toclevel2 \" style=\"text-align:justify;\"><span style=\"color:#993300;\"><strong>\u2666\u00a0Write-Verbose<\/strong><\/span><\/h5>\n<ul style=\"text-align:justify;\">\n<li class=\"toclevel2 \" style=\"text-align:justify;\">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.<\/li>\n<li class=\"toclevel2 \" style=\"text-align:justify;\">By default, the verbose message stream is not displayed, but you can display it by changing the value of the <strong>$VerbosePreference variable<\/strong> or using the Verbose common parameter in any command.<\/li>\n<li class=\"toclevel2 \" style=\"text-align:justify;\"><strong>Example:<\/strong><\/li>\n<\/ul>\n<pre style=\"padding-left:60px;\">$VerbosePreference = \"Continue\"\nPS C:\\&gt;Write-Verbose \"Copying file $filename\"<\/pre>\n<h5 class=\"toclevel2 \" style=\"text-align:justify;\"><span style=\"color:#993300;\"><strong>\u2666\u00a0Write-Warning<\/strong><\/span><\/h5>\n<ul style=\"text-align:justify;\">\n<li class=\"toclevel2 \" style=\"text-align:justify;\">The Write-Warning cmdlet writes a warning message to the Windows PowerShell host.<\/li>\n<li class=\"toclevel2 \" style=\"text-align:justify;\">The response to the warning depends on the value of the user&#8217;s <strong>$WarningPreference variable<\/strong> and the use of the WarningAction common parameter.<\/li>\n<li class=\"toclevel2 \" style=\"text-align:justify;\"><strong>Example:<\/strong><\/li>\n<\/ul>\n<pre style=\"padding-left:60px;\">write-warning \"This is only a test warning.\"<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement:\u00a0People often use Write-Host in their script, but it is considered harmful. Use other options instead. Solution:\u00a0Write-Host is always[&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/posts\/712"}],"collection":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=712"}],"version-history":[{"count":0,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=\/wp\/v2\/posts\/712\/revisions"}],"wp:attachment":[{"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pramodsingla.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}