{"id":9901,"date":"2006-07-31T17:40:00","date_gmt":"2006-07-31T17:40:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/07\/31\/suppressing-return-values-in-powershell-functions\/"},"modified":"2019-02-18T13:21:29","modified_gmt":"2019-02-18T20:21:29","slug":"suppressing-return-values-in-powershell-functions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/suppressing-return-values-in-powershell-functions\/","title":{"rendered":"Suppressing return values in PowerShell functions"},"content":{"rendered":"<p>PSMDTAG:FAQ: How do I supress return values in PowerShell functions?<\/p>\n<p>This was asked in a newgroup with the clarification:<\/p>\n<blockquote>\n<p>What I mean is:<br \/>When you call a function Foo that returns a bool, PS will write &#8216;True&#8217; or &#8216;False&#8217; to the screenby default. Is there anyway to get it to stop writing those return values?<\/p>\n<\/blockquote>\n<p dir=\"ltr\">Let&#8217;s illustrate the issue:<\/p>\n<p dir=\"ltr\"><font face=\"Courier New\" size=\"1\">PS&gt; <strong><font color=\"#000080\">function test {return $true}<br \/><\/font><\/strong>PS&gt; <strong><font color=\"#0000ff\">test<br \/><\/font><\/strong>True<br \/>PS&gt;<\/font><\/p>\n<p dir=\"ltr\">First let me clarify what is going on here by answering another FAQ:<\/p>\n<p dir=\"ltr\">PSMDTAG:FAQ: What is the difference between Write-Output $x, return $x and $x?<\/p>\n<p dir=\"ltr\"><strong><font face=\"Courier New\" color=\"#000080\">$x<\/font><\/strong> and <strong><font face=\"Courier New\" color=\"#000080\">Write-Output $x<\/font><\/strong> are exactly the same.&nbsp; Both of these emit $x to the output stream.&nbsp; (NOTE: &#8220;Write-Output&#8221;&nbsp;will show up in RC2 &#8211; in RC1 it is called&nbsp;Write-Object.)&nbsp; By default, the CONSOLE HOST&nbsp;displays anything that goes to the Output Stream on the CONSOLE.&nbsp; The engine merely makes these objects available to the host, it is the host&#8217;s decision to do what they will with them (e.g. the Exchange 2007 admin GUI takes these objects and does something completely different with them [they bind them to property sheets and display them]).<\/p>\n<p dir=\"ltr\"><font face=\"Courier New\" color=\"#000080\"><strong>Return $x<\/strong><\/font> is the same as <strong><font face=\"Courier New\" color=\"#000080\">Write-Output $x; Return<\/font><\/strong> .&nbsp; We support this syntax because it is used so widely in other environments.<\/p>\n<p dir=\"ltr\"><font face=\"Courier New\" size=\"1\">PS&gt; <font color=\"#000080\"><strong>function test1<br \/>&gt;&gt; {&nbsp; &#8220;test1&#8221;<br \/>&gt;&gt;&nbsp;&nbsp;&nbsp; write-Output &#8220;test2&#8221;<br \/>&gt;&gt;&nbsp;&nbsp;&nbsp; return &#8220;test3&#8221;<br \/>&gt;&gt; }<br \/><\/strong><\/font>&gt;&gt;<br \/>PS&gt; <strong><font color=\"#000080\">test1<br \/><\/font><\/strong>test1<br \/>test2<br \/>test3<br \/>PS&gt; <strong><font color=\"#000080\">$x = test1<br \/><\/font><\/strong>PS&gt; <strong><font color=\"#000080\">$x.length<br \/><\/font><\/strong>3<br \/>PS&gt; <strong><font color=\"#000080\">$x[0]<br \/><\/font><\/strong>test1<br \/>PS&gt; <strong><font color=\"#000080\">$x[1]<br \/><\/font><\/strong>test2<br \/>PS&gt; <strong><font color=\"#000080\">$x[2]<br \/><\/font><\/strong>test3<br \/>PS&gt;<\/font><\/p>\n<p dir=\"ltr\"><font face=\"Times New Roman\">Now back to the original question &#8211; can this be supressed?&nbsp; There are a number of ways to do this but they all entail one or another versions of the&nbsp;following strategy: Don&#8217;t&nbsp;let the Output&nbsp;stream get to the host.&nbsp; Here are some examples:<\/font><\/p>\n<p dir=\"ltr\"><font face=\"Courier New\" size=\"1\">PS&gt; #Refresher &#8211; this is what we don&#8217;t want to happen<br \/>PS&gt; <strong><font color=\"#000080\">test<br \/><\/font><\/strong>True<br \/>PS&gt; # We can assign the output to a variable<br \/>PS&gt; <font color=\"#0000ff\"><strong>$null = test<br \/><\/strong><\/font>PS&gt; # we can cast the expression to void<br \/>PS&gt; <font color=\"#0000ff\"><strong>[void](test)<br \/><\/strong><\/font>PS&gt; # We can pipel the objects to Out-Null<br \/>PS&gt; <strong><font color=\"#0000ff\">test |out-null<\/font><\/strong><\/font><\/p>\n<p dir=\"ltr\">Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/Aspen Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n<p dir=\"ltr\">&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PSMDTAG:FAQ: How do I supress return values in PowerShell functions? This was asked in a newgroup with the clarification: What I mean is:When you call a function Foo that returns a bool, PS will write &#8216;True&#8217; or &#8216;False&#8217; to the screenby default. Is there anyway to get it to stop writing those return values? Let&#8217;s [&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":[10,51,52],"class_list":["post-9901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-faq","tag-functions","tag-return-values"],"acf":[],"blog_post_summary":"<p>PSMDTAG:FAQ: How do I supress return values in PowerShell functions? This was asked in a newgroup with the clarification: What I mean is:When you call a function Foo that returns a bool, PS will write &#8216;True&#8217; or &#8216;False&#8217; to the screenby default. Is there anyway to get it to stop writing those return values? Let&#8217;s [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9901","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=9901"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9901\/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=9901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=9901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=9901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}