{"id":4011,"date":"2009-05-20T15:56:02","date_gmt":"2009-05-20T15:56:02","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/05\/20\/v2-quick-tips-finding-extra-outputs-from-your-script\/"},"modified":"2019-02-18T13:12:38","modified_gmt":"2019-02-18T20:12:38","slug":"v2-quick-tips-finding-extra-outputs-from-your-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/v2-quick-tips-finding-extra-outputs-from-your-script\/","title":{"rendered":"V2 Quick Tips: Finding Extra Outputs From Your Script"},"content":{"rendered":"<p>One of the nifty features of functions in PowerShell is that they can return as many results from as many places as you\u2019d like.&#160; In fact, any method or cmdlet that would give you an output if you try it on the command line will also return values from a function.&#160; While this can give you a lot of powerful capabilities (like easily building functions that return a list of items), it can also be a source of pain, especially if you have a decently sized script that, every once in a while, produces an unexpected output mixed with expected output.&#160; Since you cannot restrict what types get outputted from a function,and since commands in PowerShell almost always end up in a pipeline with other commands, these subtle unexpected outputs can cause big problems later on.<\/p>\n<p>Recently, a support engineer asked our discussion list how to track down these extra outputs.&#160; I pointed him to a nifty trick with the V2 debugger.<\/p>\n<p>You see, every command that outputs will call the command Out-Default.&#160; The Integrated Scripting Environment may make it easy to set line breakpoints (its F9), but the debugger in PowerShell V2 can also break on any command, including commands that aren\u2019t explicitly in your script.&#160; By setting a breakpoint on Out-Default and simply running through my script, I can actually find all of the points it outputs.<\/p>\n<p>I do this with the following line:<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #ff4500\">$null<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">Set-PSBreakpoint<\/span> <span style=\"color: #000080\">-Command<\/span> <span style=\"color: #8a2be2\">Out-Default<\/span><\/pre>\n<p>Notice the $null = ?. That&#8217;s there to suppress the natural output of Set-PSBreakpoint, which is a breakpoint object.&#160; If I don\u2019t put this there, then Set-PSBreakpoint would output, which would call Out-Default, which would break into the debugger.&#160; Whenever I find the line that I don\u2019t want to output, I simply put <span style=\"color: #ff4500\">$null<\/span> <span style=\"color: #a9a9a9\">=<\/span> at the beginning of that line.<\/p>\n<p>After I\u2019ve set this breakpoint, I can just run my script.&#160; As I run it, whenever it outputs it will break on Out-Default, and I can simply step over to the next line (F10).&#160; This will highlight the line <em>directly after <\/em>the line that outputted.&#160; If you didn\u2019t expect one of these lines to generate output, simply put <span style=\"color: #ff4500\">$null<\/span> <span style=\"color: #a9a9a9\">=<\/span> at the beginning of that line.<\/p>\n<p>Hope this Helps,<\/p>\n<p>James Brundage [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the nifty features of functions in PowerShell is that they can return as many results from as many places as you\u2019d like.&#160; In fact, any method or cmdlet that would give you an output if you try it on the command line will also return values from a function.&#160; While this can give [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[137,334],"class_list":["post-4011","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-ctp3","tag-v2quicktip"],"acf":[],"blog_post_summary":"<p>One of the nifty features of functions in PowerShell is that they can return as many results from as many places as you\u2019d like.&#160; In fact, any method or cmdlet that would give you an output if you try it on the command line will also return values from a function.&#160; While this can give [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4011"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4011\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}