{"id":12161,"date":"2011-11-07T00:01:00","date_gmt":"2011-11-07T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/11\/07\/two-powerful-tricks-for-finding-powershell-scripts\/"},"modified":"2011-11-07T00:01:00","modified_gmt":"2011-11-07T00:01:00","slug":"two-powerful-tricks-for-finding-powershell-scripts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/two-powerful-tricks-for-finding-powershell-scripts\/","title":{"rendered":"Two Powerful Tricks for Finding PowerShell Scripts"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Microsoft Scripting Guy Ed Wilson shares two powerful tricks for finding Windows PowerShell scripts.<\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span><\/span>Hey, Scripting Guy! I am wondering if you have any cool ways to help me find the scripts I have written. I have quite a few scripts in my scripts folder, but opening Windows Explorer and searching through them is slow, cumbersome, and time consuming. I am wondering if a guy like you has any cool tricks you would be willing to share to help me be more efficient. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&mdash;EB<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"font-family: Segoe\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span>Hello EB, <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Microsoft Scripting Guy Ed Wilson here. Last week, the Scripting Wife and I had a great luncheon meeting with Microsoft Windows PowerShell MVP Jim Christopher while we were in Charlotte, North Carolina. The subject of the meeting was starting a Windows PowerShell user group in Charlotte, North Carolina. Yes, that is right; we are going to start a new Windows PowerShell user group in Charlotte. It was a really cool meeting that ended up going on for several hours. Jim is a really great person, and he has a lot of great ideas. Right now, it looks like the first meeting will be in January, but stay tuned. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">If you live in the southeastern section of the United States, you may want to be making travel plans to attend the inaugural meeting. It will be cool! I have reserved a room at the Microsoft office in Charlotte, but it only holds 200 people. So this user group meeting will definitely be on a first come, first served basis. I am certain we will also have Live Meeting set up for the event. I am really psyched. The Scripting Wife is helping Jim get everything set up, so stay tuned for Scripting Wife blog posts about the experience. This is really going to be great; I could go on and on with superlatives, but EB, I need to answer your question. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">EB, I have a very cool solution to help you find and work with your Windows PowerShell scripts in a more efficient manner. It is a two-part solution. Here are the two steps:<\/span><\/p>\n<ol>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Add your script directory to the path by using $env:path. Make this addition part of your Windows PowerShell profile.<\/span><\/span><\/li>\n<li><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Use the Get-Command cmdlet to find your scripts.<\/span><\/span><\/li>\n<\/ol>\n<p><span style=\"font-family: Segoe;font-size: small\">The first thing to do is to add your script directory to the path. The nice thing about using the environment PSDrive is that changes happen immediately. There is not a need to close and open Windows PowerShell. The other nice thing about using the environment PSDrive is that changes are not permanent. It is therefore possible to add the scripts directory to the path, but not make any permanent changes to the path. Each folder in the path uses a semicolon delimiter. The final folder does not have a semicolon after it. In the following figure, I first query the existing path. Next, I add the C:\\fso folder to the path. The final command I use checks the path to see if the new folder appended correctly. The three commands are shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$env:path<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$env:path +=&#8217;;c:\\fso&#8217;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$env:path<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The commands and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6661.hsg-11-7-11-01.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of commands and associated output\" alt=\"Image of commands and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6661.hsg-11-7-11-01.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">After I have added the scripts folder (C:\\fso in this example), I can use the <b>Get-Command<\/b> cmdlet to find all the scripts in the folder. To do this, I use the <b>Get-Command<\/b> cmdlet with the <b>commandtype<\/b><i> <\/i>of <b>externalscript<\/b>. This command is shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-Command -CommandType externalscript<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Any Windows PowerShell script from any folder that appears in the <b>path<\/b> is shown in the output. The command and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1325.hsg-11-7-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1325.hsg-11-7-11-02.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Interestingly enough, the <b>definition<\/b><i> <\/i>property contains the path to the script itself. The default output does not display script contents. If I want to limit the number of scripts returned by the command, I can use the <i>totalcount <\/i>parameter. The following command returns only one Windows PowerShell script:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-Command -CommandType externalscript -TotalCount 1<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">To get an idea of the information that the <b>Get-Command<\/b> cmdlet is able to return, send the output to the <b>Format-List<\/b> cmdlet and choose all properties:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-Command -CommandType externalscript -TotalCount 1 | format-list -Property *<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The command and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3630.hsg-11-7-11-03.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3630.hsg-11-7-11-03.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The <b>ScriptBlock<\/b><i> <\/i>and the <b>ScriptContents<\/b><i> <\/i>properties contain useful information. I can therefore use the <b>Get-Content<\/b> cmdlet to search my scripts that contain a function named <b>addone<\/b>. This command and output are shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">PS C:\\&gt; Get-Command -CommandType externalscript | Where-Object { $_.scriptblock -match &#8216;addone&#8217;}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"text-decoration: underline\">CommandType<\/span>&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"text-decoration: underline\">Name<\/span>&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; <span style=\"text-decoration: underline\">Definition<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">ExternalScript&nbsp; a.ps1&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:\\fso\\a.ps1<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">By using aliases, I can shorten the command syntax a good deal. Here is the short version of the command (I use the alias <b>gcm<\/b> for <b>Get-Command<\/b>, and the alias <b>?<\/b> for the <b>Where-Object<\/b> cmdlet; I also use the <b>&ndash;c<\/b> as a short form of <b>commandtype<\/b>):<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">gcm -c externalscript | ? { $_.scriptblock -match &#8216;addone&#8217;}<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Unfortunately, I cannot use neither an alias for the <b>externalscript<\/b> enumeration nor a wildcard character. In the following figure, I attempt to use a wildcard character to avoid typing <i>externalscript.<\/i> The command and associated error are shown.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3000.hsg-11-7-11-04.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated error\" alt=\"Image of command and associated error\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3000.hsg-11-7-11-04.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Because I now have the enumeration name, I can use my <b>Get-EnumValues<\/b> function from my <\/span><a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-enumerations-and-values\/\"><span style=\"font-family: Segoe\">Enumerations and Values<\/span><\/a><span style=\"font-family: Segoe\"> blog post. By using the <b>Get-Enumvalues<\/b> function, I discover that <b>externalscript<\/b><i> <\/i>has a value of 16. This is shown in the following figure.<\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6560.hsg-11-7-11-05.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image showing externalscript has value of 16\" alt=\"Image showing externalscript has value of 16\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6560.hsg-11-7-11-05.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Cool! Now that I know I can use an enumeration value, I can really shorten the command:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">gcm -c 16<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">This command is short enough such that it is really easy to type. For example, if I am looking for my function that creates a new temporary file, but I am not certain of the exact name, I can use the following to find it:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">PS C:\\&gt; gcm -c 16 | ? { $_.scriptblock -match &#8216;tmp&#8217;}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\"><span style=\"text-decoration: underline\">CommandType<\/span>&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"text-decoration: underline\">Name<\/span>&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; <span style=\"text-decoration: underline\">Definition<\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">ExternalScript&nbsp; Out-TempFile.ps1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c:\\fso\\Out-TempFile.ps1<b><\/b><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">EB, that is all there is to adding a folder to the path and using the <b>Get-Command<\/b> cmdlet to find scripts. Because the <b>match<\/b><i> <\/i>operator accepts a regular expression, I have a really powerful way to search my Windows PowerShell scripts to find exactly what I am looking for. Join me tomorrow for more Windows PowerShell goodness&mdash;you will be glad you did!<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I invite you to follow me on <\/span><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><span style=\"font-family: Segoe\">Twitter<\/span><\/a><span style=\"font-family: Segoe\"> and <\/span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"font-family: Segoe\">Facebook<\/span><\/a><span style=\"font-family: Segoe\">. If you have any questions, send email to me at <\/span><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><span style=\"font-family: Segoe\">scripter@microsoft.com<\/span><\/a><span style=\"font-family: Segoe\">, or post your questions on the <\/span><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><span style=\"font-family: Segoe\">Official Scripting Guys Forum<\/span><\/a><span style=\"font-family: Segoe\">. See you tomorrow. Until then, peace.<\/span><\/span><\/p>\n<\/p>\n<p><span style=\"font-size: small\"><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><\/span>&nbsp;<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy Ed Wilson shares two powerful tricks for finding Windows PowerShell scripts. &nbsp; Hey, Scripting Guy! I am wondering if you have any cool ways to help me find the scripts I have written. I have quite a few scripts in my scripts folder, but opening Windows Explorer and searching through them [&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-12161","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 shares two powerful tricks for finding Windows PowerShell scripts. &nbsp; Hey, Scripting Guy! I am wondering if you have any cool ways to help me find the scripts I have written. I have quite a few scripts in my scripts folder, but opening Windows Explorer and searching through them [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12161","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=12161"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12161\/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=12161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}