{"id":916,"date":"2014-07-29T00:01:00","date_gmt":"2014-07-29T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/29\/troubleshoot-powershell-script-output\/"},"modified":"2014-07-29T00:01:00","modified_gmt":"2014-07-29T00:01:00","slug":"troubleshoot-powershell-script-output","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/troubleshoot-powershell-script-output\/","title":{"rendered":"Troubleshoot PowerShell Script Output"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about troubleshooting output from a Windows PowerShell script.<\/span>\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy! First, I want to say that you guys absolutely rock! I mean, I never miss a single day reading your posts. They are always right on, and it is like somehow you read my mind. I cannot tell you how many times I have read your post in the morning, and then needed to do that very thing in the afternoon. I also know that you probably get hundreds of emails every day, so I don&rsquo;t really expect an answer, but I thought that maybe&#8230;what the heck. So here goes:\nI wrote a script that works perfectly. I am trying to reduce our security footprint by stopping services. For some reason, the number of services has exploded between Windows Server&nbsp;2008 and Windows Server&nbsp;2012&nbsp;R2. I think, &#8220;The fewer things running, the better.&#8221; So I am trying to see what I have by doing an audit. I thought if I get a list of what is running and what is not, I could compare them to see what is going on. Here is the script:<\/p>\n<p style=\"margin-left:30px\">$services = Get-WmiObject win32_Service<\/p>\n<p style=\"margin-left:30px\">$running = $services | where { $_.state -eq &#8216;running&#8217; }<\/p>\n<p style=\"margin-left:30px\">$stopped = $services | where { $_.state -eq &#8216;stopped&#8217; }<\/p>\n<p style=\"margin-left:30px\">Write-Host -ForegroundColor Green &#8220;There are &#8221; $running.Count &#8221; running services. They are here:&#8221;<\/p>\n<p style=\"margin-left:30px\">$running | Format-Table name<\/p>\n<p style=\"margin-left:30px\">Write-Host -ForegroundColor Red &#8220;There are &#8221; $stopped.Count &#8221; stopped services. They are here:&#8221;<\/p>\n<p style=\"margin-left:30px\">$stopped | Format-Table name\nThe problem is not with the script, it works perfectly for what I want to do. The problem is that I want to write the data to a text file. Here is what I did, and it sort of works, but not really:<\/p>\n<p style=\"margin-left:30px\">$file = &#8220;c:fsoMyServiceStatus.txt&#8221;<\/p>\n<p style=\"margin-left:30px\">$services = Get-WmiObject win32_Service<\/p>\n<p style=\"margin-left:30px\">$running = $services | where { $_.state -eq &#8216;running&#8217; }<\/p>\n<p style=\"margin-left:30px\">$stopped = $services | where { $_.state -eq &#8216;stopped&#8217; }<\/p>\n<p style=\"margin-left:30px\">Write-Host -ForegroundColor Green &#8220;There are &#8221; $running.Count &#8221; running services. They are here:&#8221; |<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Out-file $file -Append<\/p>\n<p style=\"margin-left:30px\">$running | Format-Table name |<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Out-file $file -Append<\/p>\n<p style=\"margin-left:30px\">Write-Host -ForegroundColor Red &#8220;There are &#8221; $stopped.Count &#8221; stopped services. They are here:&#8221;&nbsp; |<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Out-file $file -Append<\/p>\n<p style=\"margin-left:30px\">$stopped | Format-Table name |<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Out-file $file -Append&nbsp;\nWhen I say it sort of works, I mean that it creates the text file, and it writes all of the service names to the file. But I do not get my header information. So I cannot really tell which services are running and which are stopped. It is really weird, and I am wondering if I have found a bug in Windows PowerShell. If you don&#8217;t have an answer, maybe you can forward this to someone who can look at it. Thanks again for all you do.\n&mdash;BP\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello BP,\nMicrosoft Scripting Guy, Ed Wilson, is here. This morning I am sitting in the living room sipping a cup of Earle Grey tea with a bit of jasmine in it. I am watching the rain coming down, and looking over my <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a> email.\nBP, yes, I do get a lot of email. I also enjoy answering some of it. I am glad you enjoy reading my column and that you have found it useful. I am especially pleased that you are writing your own scripts and using Windows PowerShell to solve some of your issues.<\/p>\n<h2>Understanding different output streams<\/h2>\n<p>One concept that is a bit difficult to understand in Windows PowerShell is the concept of output streams. Windows PowerShell has several output streams. This can be especially useful for things like trying to redirect errors, verbose output, debug messages, or even warning messages. However, it is not useful for redirecting the <b>Write-Host<\/b> cmdlet.<\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp; For a good discussion about the various output streams and redirecting output, see <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/understanding-streams-redirection-and-write-host-in-powershell\/\" target=\"_blank\">Understanding Streams, Redirection, and Write-Host in PowerShell<\/a>, which was written by Honorary Scripting Guy, June Blender.\nBasically what happens when I use <b>Write-Host<\/b> is that it goes only to the host (usually the Windows PowerShell console). Because <b>Write-Host<\/b> does not go to any of the output streams, I am unable to write the results to a text file.<\/p>\n<h2>Output stuff<\/h2>\n<p><b>Write-Host<\/b> is a useful cmdlet to precisely produce messages to the Windows PowerShell console because it does not write to an output stream. This means that I can use <b>Write-Host<\/b> to display information in an interactive fashion to a person running a Windows PowerShell script, and this information will not go on to an output file. For example, if I want to prompt a user to &ldquo;press any key to begin&rdquo; I can use <b>Write-Host<\/b>. Or I can use <b>Write-Host<\/b> to display status messages such as &ldquo;counting services&rdquo; or some other such prompt.\nOf course, I can also use <b>Read-Host<\/b> and <b>Write-Progress<\/b> to perform these types of tasks. Often in Windows PowerShell, I have more than one way of doing things. One of the things you may want to read about is Windows PowerShell best practices. I have written a collection of posts where I talk about various aspects of Windows PowerShell scripting. For more information, see these <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/best+practices\/default.aspx\" target=\"_blank\">Hey, Scripting Guy! Blog posts<\/a>.\nOne hint as to what might be happening with your script is that after I run it, the console output box only contains the results from the two <b>Write-Host<\/b> cmdlets. Everything else writes to the text file.<\/p>\n<h2>The easy change<\/h2>\n<p>The easiest fix to your script is to simply change <b>Write-Host<\/b> to <b>Write-Output<\/b>. Of course, when you do that, you also need to delete the <b>&ndash;ForegroundColor<\/b> portion of your script. Now when the script runs, it will write everything to the text file and nothing to the screen.\nIf you need to output to both the screen and the text file, you can use the <b>Tee-Object<\/b> cmdlet. For more information about <b>Tee-Object<\/b>, read <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2009\/07\/07\/hey-scripting-guy-how-can-i-both-save-information-in-a-file-and-display-it-on-the-screen.aspx\" target=\"_blank\">How Can I Both Save Information in a File and Display It on the Screen?<\/a>\nThere are a number of other changes that could be made to the script to make it a bit leaner, but that is a subject for another post.\nBP, that is all there is to troubleshooting script output. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about troubleshooting output from a Windows PowerShell script. &nbsp;Hey, Scripting Guy! First, I want to say that you guys absolutely rock! I mean, I never miss a single day reading your posts. They are always right on, and it is like somehow you read my mind. I cannot [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,134,45],"class_list":["post-916","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-troubleshooting","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about troubleshooting output from a Windows PowerShell script. &nbsp;Hey, Scripting Guy! First, I want to say that you guys absolutely rock! I mean, I never miss a single day reading your posts. They are always right on, and it is like somehow you read my mind. I cannot [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=916"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/916\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}