{"id":4055,"date":"2013-03-07T00:01:00","date_gmt":"2013-03-07T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/03\/07\/working-with-the-powershell-alias-drive\/"},"modified":"2013-03-07T00:01:00","modified_gmt":"2013-03-07T00:01:00","slug":"working-with-the-powershell-alias-drive","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/working-with-the-powershell-alias-drive\/","title":{"rendered":"Working with the PowerShell Alias: Drive"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about how to work with the Windows PowerShell Alias: drive.<\/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! Can you tell me what an Alias: drive is? Is it anything like a virtual drive?<\/p>\n<p>&mdash;EC<\/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 EC,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today we have the Windows PowerShell User Group meeting in Charlotte, North Carolina at the Microsoft office. The Scripting Wife has been talking about the meeting all week, and there are several new people signed up to attend. It will be fun. We are planning to do another miniscripting games event. Those are always fun. It is not too late to sign up. You can find details on the <a href=\"http:\/\/powershellgroup.org\/charlotte.nc\" target=\"_blank\">Charlotte PowerShell Users Group site<\/a>.<\/p>\n<p>EC, the Alias: drive is a virtual drive&mdash;just not in the way you are probably thinking.<\/p>\n<h2>What is the Alias: drive?<\/h2>\n<p>The Alias: drive is a drive that is created by the Windows PowerShell provider subsystem to expose information about aliases in a way that allows you to interact with aliases in the same way you would work with the file system. This sounds more confusing than it really is. For example, to get to the Alias: drive, I use the <strong>Set-Location<\/strong> cmdlet (or I can use the alias <strong>cd<\/strong><em> <\/em>or <strong>sl<\/strong>). This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; cd alias:<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt;<\/p>\n<p>When I am on the Alias: drive, I can use the <strong>Get-ChildItem<\/strong> cmdlet (or the alias <strong>dir<\/strong><em> <\/em>or <strong>ls<\/strong><em>) <\/em>to obtain a list of all the aliases that are defined on my computer as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt; dir<\/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\/6683.HSG-3-7-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6683.HSG-3-7-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>To examine all of the properties of a specific alias, use the <strong>Get-Item<\/strong> cmdlet to return information about the alias. Then pipe the results to the <strong>Format-List<\/strong> cmdlet and choose the * wildcard for the properties. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt; Get-Item r | fl *<\/p>\n<p>The command and the output associated with the command are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3487.HSG-3-7-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3487.HSG-3-7-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>When attempting to understand more about aliases, the crucial things to pay attention to are the <strong>Definition<\/strong> and the <strong>Options<\/strong> properties.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note&nbsp;&nbsp;&nbsp;<\/strong>The command that the alias resolves to is actually available in a number of properties. These properties are <strong>ResolvedCommandName<\/strong>, <strong>ReferencedCommand<\/strong>, <strong>ResolvedCommand<\/strong>, and <strong>Definition<\/strong>.<\/p>\n<p>To find all of the aliases that are defined for a particular cmdlet (one that I use on a regular basis so that it would pay great dividends on reducing typing workload), I pipe the results of the <strong>Get-ChildItem<\/strong> cmdlet on the Alias: drive and filter on the <strong>Definition<\/strong> property for the cmdlet. This technique is shown here (<strong>dir<\/strong> is an alias for <strong>Get-ChildItem<\/strong>).<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt; dir | ? definition -eq &#8216;get-childitem&#8217;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">CommandType&nbsp;&nbsp;&nbsp;&nbsp; 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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ModuleName<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp; &#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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Alias&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dir -&gt; Get-ChildItem<\/p>\n<p style=\"padding-left: 30px\">Alias&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gci -&gt; Get-ChildItem<\/p>\n<p style=\"padding-left: 30px\">Alias&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ls -&gt; Get-ChildItem<\/p>\n<h2>Looking at alias options<\/h2>\n<p>Several cmdlets have ReadOnly aliases. These appear when I look at the <strong>Options<\/strong> property. All of the ReadOnly aliases are also defined as available to all scopes within Windows PowerShell. Some aliases are not ReadOnly, but most of the not ReadOnly aliases are defined for all scopes. On my computer, three aliases have no options. Because working with the Alias: drive returns objects, this type of information is really easy to obtain. I use the <strong>Get-Childitem<\/strong> cmdlet to return all aliases from the Alias: drive. I pipe the resulting <strong>AliasInfo<\/strong> objects to the <strong>Sort<\/strong> object cmdlet, and I sort on the <strong>Options<\/strong> property. I then group the objects by piping to the <strong>Group-Object<\/strong> cmdlet. The command and output associated with the command are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt; dir | sort options | group options<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Count Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Group<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; 3 None&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {ep, sls, gmh}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; 48 AllScope&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {exsn, set, popd, ipsn&#8230;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; 99 ReadOnly, AllScope&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {rdr, ni, nv, npssc&#8230;}<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;When working with the Alias: drive, it does not automatically import modules. Therefore, the results are limited to only the modules that I have imported.<\/p>\n<p>If I want to know about all of my modules and cmdlets, I need to first import all of the modules. This command and output are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt; gmo -ListAvailable | ipmo<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt; dir | sort options | group options<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Count Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Group<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; 72 None&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {Get-ProvisionedAppxPackage, gfsd, Get-GPPermissi&#8230;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; 48 AllScope&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {lp, rd, ren, rjb&#8230;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 111 ReadOnly, AllScope&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {rcms, rcsn, tee, swmi&#8230;}<\/p>\n<p>To delete an alias that is not marked as ReadOnly, all I need to do is use the <strong>Remove-Ite<\/strong>m cmdlet. When I do this, the <strong>Get-Item<\/strong> cmdlet informs me that I was successful. These commands are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt; Remove-Item lp<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt; Get-Item lp<\/p>\n<p style=\"padding-left: 30px\">Get-Item : Cannot find path &#8216;Alias:\\lp&#8217; because it does not exist.<\/p>\n<p style=\"padding-left: 30px\">At line:1 char:1<\/p>\n<p style=\"padding-left: 30px\">+ Get-Item lp<\/p>\n<p style=\"padding-left: 30px\">+ ~~~~~~~~~~~<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ObjectNotFound: (Alias:\\lp:String) [Get-Item], ItemNo<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; tFoundException<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCom<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; Mand<\/p>\n<p>If I attempt to delete a ReadOnly alias, an error arises as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt; Remove-Item rcms<\/p>\n<p style=\"padding-left: 30px\">Remove-Item : Alias was not removed because alias rcms is constant or read-only.<\/p>\n<p style=\"padding-left: 30px\">At line:1 char:1<\/p>\n<p style=\"padding-left: 30px\">+ Remove-Item rcms<\/p>\n<p style=\"padding-left: 30px\">+ ~~~~~~~~~~~~~~~~<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : WriteError: (rcms:String) [Remove-Item], SessionState<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; UnauthorizedAccessException<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : AliasNotRemovable,Microsoft.PowerShell.Commands.Remov<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; eItemCommand<\/p>\n<p>However, if I use the <strong>&ndash;force<\/strong><em> <\/em>switched parameter when I attempt to remove the ReadOnly alias, the command succeeds. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt; Remove-Item rcms -Force<\/p>\n<p style=\"padding-left: 30px\">PS Alias:\\&gt; Get-Alias rcms<\/p>\n<p style=\"padding-left: 30px\">Get-Alias : This command cannot find a matching alias because an alias with the<\/p>\n<p style=\"padding-left: 30px\">name &#8216;rcms&#8217; does not exist.<\/p>\n<p style=\"padding-left: 30px\">At line:1 char:1<\/p>\n<p style=\"padding-left: 30px\">+ Get-Alias rcms<\/p>\n<p style=\"padding-left: 30px\">+ ~~~~~~~~~~~~~~<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ObjectNotFound: (rcms:String) [Get-Alias], ItemNotFou<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; ndException<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.G<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; etAliasCommand<\/p>\n<p>EC, that is all there is to using the Alias: drive. Windows PowerShell Provider Week will continue tomorrow when I will talk about the Variable: drive.<\/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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about how to work with the Windows PowerShell Alias: drive. &nbsp;Hey, Scripting Guy! Can you tell me what an Alias: drive is? Is it anything like a virtual drive? &mdash;EC &nbsp;Hello EC, Microsoft Scripting Guy, Ed Wilson, is here. Today we have the Windows PowerShell User Group meeting [&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":[415,3,4,45],"class_list":["post-4055","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-psproviders","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about how to work with the Windows PowerShell Alias: drive. &nbsp;Hey, Scripting Guy! Can you tell me what an Alias: drive is? Is it anything like a virtual drive? &mdash;EC &nbsp;Hello EC, Microsoft Scripting Guy, Ed Wilson, is here. Today we have the Windows PowerShell User Group meeting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4055","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=4055"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4055\/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=4055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}