{"id":3131,"date":"2013-07-31T00:01:00","date_gmt":"2013-07-31T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/07\/31\/powershell-get-command-cmdlet-returns-only-one-instance\/"},"modified":"2013-07-31T00:01:00","modified_gmt":"2013-07-31T00:01:00","slug":"powershell-get-command-cmdlet-returns-only-one-instance","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-get-command-cmdlet-returns-only-one-instance\/","title":{"rendered":"PowerShell Get-Command Cmdlet Returns Only One Instance"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, shows how to configure Windows PowerShell to return more than one instance of a command.<\/span><\/p>\n<p><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! I have a problem. I recently upgraded to Windows&nbsp;8, and now it seems that the <strong>Get-Command<\/strong> cmdlet does not work properly. In the past, I have used <strong>Get-Command<\/strong> as a replacement for Where.exe in that it is great at finding commands I need. But now in Windows 8, the <strong>Get-Command<\/strong> cmdlet only returns a single instance. What gives?<\/p>\n<p>&mdash;MC<\/p>\n<p><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 MC,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Tomorrow we have the Windows PowerShell User Group meeting at the Charlotte, North Carolina Microsoft office. If you are in town, and do not have anything going on around 6:30 P.M., stop by. It will be loads of fun, and I can promise you will learn at least one new thing.<\/p>\n<h2>Using Get-Command to find commands<\/h2>\n<p>MC, I know it may sound strange, but I also use <strong>Get-Command<\/strong> to find commands. It is great at doing that. It finds Windows commands that are in my path, and it is much faster than opening Windows Explorer, and telling it to search.<\/p>\n<p>For example, if I want to know if a command is available, I use <strong>Get-Command<\/strong> to find it for me. This is how I learned that there were two notepad.exe programs available. But when I run <strong>Get-Command<\/strong> in Windows&nbsp;8, I am greeted with the following:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7433.hsg-7-31-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7433.hsg-7-31-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Hmm&hellip;Did we get rid of one of the Notepads in Windows&nbsp;8? I don&rsquo;t know, do I? It also makes me suspect that perhaps something is going on with <strong>Get-Command<\/strong>.<\/p>\n<h2>Force additional discovery in Get-Command<\/h2>\n<p>In Windows PowerShell&nbsp;3.0, we made a change to <strong>Get-Command<\/strong>. When it finds a command, it returns. This is done for performance reasons, and results in faster searches. But if I am particularly interested in additional instances, I can force <strong>Get-Command<\/strong> to keep working. To do this, I specify a value for the <strong>TotalCount<\/strong> parameter. The value for <strong>TotalCount<\/strong> is the maximum value. So if I tell it to look for 10 instances of Notepad, it will keep trying. Of course, I only have two copies, so I am safe. Here is the command I use:<\/p>\n<p style=\"padding-left: 30px\">Get-Command notepad -TotalCount 10<\/p>\n<p>The command and its associated output are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3660.hsg-7-31-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3660.hsg-7-31-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>But where are these programs located?<\/h2>\n<p>To find where the programs reside, I need a bit more information. To find the additional information, I pipe the results to the <strong>Format-List<\/strong> cmdlet and choose all of the properties as shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-Command notepad | Format-List *<\/p>\n<p>The following image shows the command and the output associated with the command.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1145.hsg-7-31-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1145.hsg-7-31-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>The path to the executable is in the <strong>Path<\/strong> property (no surprise, right?). Therefore, all I need to do is to select the path. I do this in the following command, and I can find where both Notepads reside.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Command notepad -TotalCount 5 | Select-Object name, path<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Path<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;-<\/p>\n<p style=\"padding-left: 30px\">notepad.exe&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C:\\Windows\\SYSTEM32\\notepad.exe<\/p>\n<p style=\"padding-left: 30px\">notepad.exe&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C:\\Windows\\notepad.exe<\/p>\n<h3>Using the Path property<\/h3>\n<p>Now that I know about the <strong>Path<\/strong> property and the way <strong>Get-Command<\/strong> returns the first application it finds in the search path, I might be interested in the way the commands returns. To do this, I look at the <strong>Path<\/strong> variable from the <strong>$env<\/strong> drive. I can see that first Windows\\System32 is searched, and then the Windows directory. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; ($env:Path -split &#8216;;&#8217;)[0..1]<\/p>\n<p style=\"padding-left: 30px\">C:\\Windows\\SYSTEM32<\/p>\n<p style=\"padding-left: 30px\">C:\\Windows<\/p>\n<p>Armed with this information I can see that the Notepad from Windows\\System32 returns first (I actually saw this earlier). Therefore, the application highest in the search path returns when I do not specify <strong>TotalCount<\/strong>.<\/p>\n<p>So, now I can use the <strong>Path<\/strong> property from <strong>Get-Command<\/strong>, and I know what it will do. So if I want to look at the byte encoding of Notepad, I can use the <strong>Get-Command<\/strong> cmdlet to return the path to the application. I can supply that to the <strong>Get-Content<\/strong> cmdlet <strong>Path<\/strong> parameter, and I can specify <strong>Encoding Byte<\/strong>. I store this in the <strong>$bytes<\/strong> variable, and print out the first four bytes. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $bytes = Get-Content -Path (Get-Command notepad).path -Encoding Byte<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $bytes[0..3]<\/p>\n<p style=\"padding-left: 30px\">77<\/p>\n<p style=\"padding-left: 30px\">90<\/p>\n<p style=\"padding-left: 30px\">144<\/p>\n<p style=\"padding-left: 30px\">0<\/p>\n<p>MC, that is all there is to using the <strong>Get-Command<\/strong> cmdlet to find commands. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I 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=\"mailto: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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to configure Windows PowerShell to return more than one instance of a command. &nbsp;Hey, Scripting Guy! I have a problem. I recently upgraded to Windows&nbsp;8, and now it seems that the Get-Command cmdlet does not work properly. In the past, I have used Get-Command as a replacement [&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":[51,3,4,45],"class_list":["post-3131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to configure Windows PowerShell to return more than one instance of a command. &nbsp;Hey, Scripting Guy! I have a problem. I recently upgraded to Windows&nbsp;8, and now it seems that the Get-Command cmdlet does not work properly. In the past, I have used Get-Command as a replacement [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3131","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=3131"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3131\/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=3131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}