{"id":38,"date":"2009-09-14T22:47:44","date_gmt":"2009-09-15T04:47:44","guid":{"rendered":"http:\/\/jameskovacs.com\/2009\/09\/15\/PowerShell+Tip+Providers"},"modified":"2009-09-14T22:47:44","modified_gmt":"2009-09-15T04:47:44","slug":"powershell-tip-providers","status":"publish","type":"post","link":"https:\/\/www.jameskovacs.com\/index.php\/2009\/09\/14\/powershell-tip-providers\/","title":{"rendered":"PowerShell Tip: Providers"},"content":{"rendered":"<p>At first glance, PowerShell appears to be yet another command shell with the interesting twist that you pipe objects between commands rather than strings. But there is more to PowerShell than that. One fascinating area is PowerShell Providers. (PowerShell Providers aren\u2019t anything new as they\u2019ve been there since v1. So I\u2019m not the first \u2013 nor will I be the last \u2013 to blog about them, but hopefully some folks starting out with PowerShell find this useful\u2026)<\/p>\n<p>We\u2019ll start with a simple example using \u201cls\u201d to list the contents of a directory:<\/p>\n<p>ls c:\\<\/p>\n<p>Now \u201cls\u201d is just a two-letter version of \u201cdir\u201d and both are aliases for \u201cGet-ChildItem\u201d. How do I know that?<\/p>\n<p>ls alias:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"\/wp-content\/uploads\/WindowsLiveWriter\/PowerShellProviders_C8DC\/image_31b63a7e-e40b-4505-8af9-7f0e10fb08bc.png\" width=\"781\" height=\"379\">   <\/p>\n<p>This prints out all current aliases. That funky \u201calias:\u201d is a PowerShell provider. If you want a specific alias, you can \u201cls alias:ls\u201d or \u201cls alias:dir\u201d.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"\/wp-content\/uploads\/WindowsLiveWriter\/PowerShellProviders_C8DC\/image_6c78cab1-6aa6-4503-8114-8b6562aee871.png\" width=\"733\" height=\"295\"> <\/p>\n<p>To get a list of currently installed PowerShell providers, you can use <strong>Get-PSProvider<\/strong>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"\/wp-content\/uploads\/WindowsLiveWriter\/PowerShellProviders_C8DC\/image_827bbdd5-a9b7-4400-9435-0e712a60e90f.png\" width=\"781\" height=\"283\"> <\/p>\n<p>You should notice a few interesting entries there. You want a list of environment variables? The Environment provider does the trick:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"\/wp-content\/uploads\/WindowsLiveWriter\/PowerShellProviders_C8DC\/image_6be74221-d00d-4d7b-8737-2d5130bad4cc.png\" width=\"1029\" height=\"662\"> <\/p>\n<p>Note that providers aren\u2019t read-only. Let\u2019s say you want to temporarily add a directory to your path. In cmd.exe, you would do the following:<\/p>\n<p>set PATH = %PATH%;&lt;EXTRA_DIR&gt;;<\/p>\n<p>In PowerShell, you use the Environment provider:<\/p>\n<p>$env:PATH += &#8220;;&lt;EXTRA_DIR&gt;&#8221;;&#8221;<\/p>\n<p>Notice the env: prefix that tells PowerShell that the variable is handled by the Environment provider. (Notice above that \u201cEnv\u201d is listed as the \u201cdrive\u201d for the Environment provider.)<\/p>\n<p>Let\u2019s explore some more\u2026<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"\/wp-content\/uploads\/WindowsLiveWriter\/PowerShellProviders_C8DC\/image_b4f1d0c9-ddb5-4521-bece-02ca951a09f6.png\" width=\"1029\" height=\"614\"> <\/p>\n<p>Notice that changing drive letters is actually handled by the Function provider and are just commands.<\/p>\n<p>It gets more interesting with the Registry provider through which you can access HKEY_LOCAL_MACHINE via hklm: and HKEY_CURRENT_USER via hkcu:.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"\/wp-content\/uploads\/WindowsLiveWriter\/PowerShellProviders_C8DC\/image_38a35500-6041-46af-8921-39f5927dce2c.png\" width=\"573\" height=\"331\"> <\/p>\n<p>You even get tab completion while typing. (Try ls hkcu:&lt;TAB&gt;&lt;TAB&gt;&lt;TAB&gt; to see various subkeys for HKEY_CURRENT_USER.) And assuming that you have write permission to the registry keys, you can set them too!<\/p>\n<p>PowerShell providers aren\u2019t limited to those shipped by Microsoft. You can in fact <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms714636(VS.85).aspx\" target=\"_blank\" rel=\"noopener\">write your own<\/a>, though I\u2019ve never tried it. People have written their own providers for everything from SharePoint to Subversion.<\/p>\n<p>So go check out PowerShell providers. Happy Scripting!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At first glance, PowerShell appears to be yet another command shell with the interesting twist that you pipe objects between commands rather than strings. But there is more to PowerShell than that. One fascinating area is PowerShell Providers. (PowerShell Providers aren\u2019t anything new as they\u2019ve been there since v1. So I\u2019m not the first \u2013 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":0,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}