{"id":2791,"date":"2010-12-10T01:28:00","date_gmt":"2010-12-10T01:28:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2010\/12\/10\/same-command-different-return-types\/"},"modified":"2019-02-18T13:05:51","modified_gmt":"2019-02-18T20:05:51","slug":"same-command-different-return-types","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/same-command-different-return-types\/","title":{"rendered":"Same Command. Different Return Types."},"content":{"rendered":"<p><i>My command seems to behave differently depending on how many items were returned. <\/i><\/p>\n<p>This is something that the PowerShell Team hears from the community or our internal partners every couple of weeks. This blog post will explain when a Windows PowerShell command returns different types and offer some reasons for why it is done this way.<\/p>\n<p>In general, Windows PowerShell commands can return one of three things: <\/p>\n<ol>\n<li>Null (not really a thing, I know) <\/li>\n<li>A single object of a particular type <\/li>\n<li>An array of objects (Object[]) <\/li>\n<\/ol>\n<p>Executing the commands below returned each of the types listed above.&#160; <\/p>\n<blockquote>\n<p><font face=\"Lucida Console\">(<font color=\"#0000ff\">Get-Service<\/font> <font color=\"#9b00d3\">foobar<\/font>).GetType()         <br \/>(<font color=\"#0000ff\">Get-Service <\/font><font color=\"#9b00d3\">NetLogon<\/font>).GetType()         <br \/>(<font color=\"#0000ff\">Get-Service<\/font> <font color=\"#8064a2\"><font color=\"#9b00d3\">Net<\/font>*<\/font>).GetType() <\/font><\/p>\n<\/blockquote>\n<p>Compare this to executing a WMI query in VBScript. Because you always get an array back, administrators have to litter their scripts with useless for-each loops. An extreme example is shown below, where the WMI query gets the Win32_OperatingSystem class to compute system uptime. It returns an array, even though there can never be multiple Win32_OperatingSystem items.<\/p>\n<blockquote>\n<p><font face=\"Lucida Console\">Set colOperatingSystems = objWMIService.ExecQuery _        <br \/>&#160;&#160;&#160; (&quot;Select * from Win32_OperatingSystem&quot;)<\/font><\/p>\n<p><font face=\"Lucida Console\">For Each objOS in colOperatingSystems        <br \/>&#160;&#160;&#160; dtmBootup = objOS.LastBootUpTime         <br \/>&#160;&#160;&#160; dtmLastBootupTime = WMIDateStringToDate(dtmBootup)         <br \/>&#160;&#160;&#160; dtmSystemUptime = DateDiff(&quot;h&quot;, dtmLastBootUpTime, Now)         <br \/>&#160;&#160;&#160; Wscript.Echo dtmSystemUptime         <br \/>Next<\/font><\/p>\n<\/blockquote>\n<p>Since Windows PowerShell is also an interactive shell, we wanted to make it very easy for Administrators to work at the command line. A set of Windows PowerShell commands that do the same uptime calculation is shown below. Note that I didn\u2019t have to do any indexing or iterating over a collection.<\/p>\n<blockquote>\n<p><font face=\"Lucida Console\"><font color=\"#ff0000\">$os<\/font> = <font color=\"#0000ff\">Get-WmiObject<\/font> <font color=\"#9b00d3\">Win32_OperatingSystem          <br \/><\/font><font color=\"#ff0000\">$lastBootup<\/font> = <font color=\"#ff0000\">$os<\/font>.ConvertToDateTime(<font color=\"#ff0000\">$os<\/font>.LastBootUpTime)         <br \/><font color=\"#ff0000\">$uptime<\/font> = ((<font color=\"#0000ff\">Get-Date<\/font>) &#8211; <font color=\"#ff0000\">$lastBootup<\/font>).TotalHours         <br \/><font color=\"#0000ff\">Write-Host<\/font> <font color=\"#ff0000\">$uptime<\/font><\/font><\/p>\n<\/blockquote>\n<p>This behavior allows for some fairly lazy, err\u2026 convenient interactive console experiences. For example, let&#8217;s say that I wanted to get the \u201cMicrosoft SharePoint Workspace Audit Service\u201d that is installed on my machine.<\/p>\n<blockquote>\n<p><font face=\"Lucida Console\"><font color=\"#0000ff\">Get-Service<\/font> \u2013<font color=\"#000080\">Name          <br \/><\/font><\/font>&#8230;       <br \/>Mcx2Svc       <br \/>Microsoft SharePoint Workspace Audit Service       <br \/>MMCSS       <br \/>MpsSvc       <br \/>\u2026<\/p>\n<\/blockquote>\n<p>I can do that with very little typing:<\/p>\n<blockquote>\n<p><font color=\"#ff0000\">$svc <\/font><font color=\"#000000\">= <\/font><font face=\"Lucida Console\"><font color=\"#0000ff\">Get-Service<\/font> <font color=\"#9b00d3\">Mi*<\/font><\/font><\/p>\n<\/blockquote>\n<p>Here I used a wildcard expression (that, in many cases, would return multiple values) but Windows PowerShell returned a single object that I can work with directly. <\/p>\n<p>On the other hand, if you\u2019re writing some scripts or managed code and want to guarantee that you get an array back, we offer the array cast operator @(). More details can be found in an <a href=\"http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2007\/01\/23\/array-literals-in-powershell.aspx?wa=wsignin1.0\">old post<\/a> by Bruce.<\/p>\n<p>In closing: we can&#8217;t (or at least shouldn&#8217;t) try to infer what you meant, but at least we can make your life easier in a lot of cases by being smart about what we send back to you.<\/p>\n<p>Travis Jones [MSFT]   <br \/>Windows PowerShell PM    <br \/>Microsoft Corporation<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My command seems to behave differently depending on how many items were returned. This is something that the PowerShell Team hears from the community or our internal partners every couple of weeks. This blog post will explain when a Windows PowerShell command returns different types and offer some reasons for why it is done this [&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,52],"class_list":["post-2791","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-faq","tag-return-values"],"acf":[],"blog_post_summary":"<p>My command seems to behave differently depending on how many items were returned. This is something that the PowerShell Team hears from the community or our internal partners every couple of weeks. This blog post will explain when a Windows PowerShell command returns different types and offer some reasons for why it is done this [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/2791","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=2791"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/2791\/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=2791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=2791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=2791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}