March 7th, 2013

Working with the PowerShell Alias: Drive

Doctor Scripto
Scripter

Summary: Microsoft Scripting Guy, Ed Wilson, talks about how to work with the Windows PowerShell Alias: drive.

Hey, Scripting Guy! Question Hey, Scripting Guy! Can you tell me what an Alias: drive is? Is it anything like a virtual drive?

—EC

Hey, Scripting Guy! Answer Hello EC,

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 Charlotte PowerShell Users Group site.

EC, the Alias: drive is a virtual drive—just not in the way you are probably thinking.

What is the Alias: drive?

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 Set-Location cmdlet (or I can use the alias cd or sl). This is shown here.

PS C:\> cd alias:

PS Alias:\>

When I am on the Alias: drive, I can use the Get-ChildItem cmdlet (or the alias dir or ls) to obtain a list of all the aliases that are defined on my computer as shown here.

PS Alias:\> dir

The command and its associated output are shown here.

Image of command output

To examine all of the properties of a specific alias, use the Get-Item cmdlet to return information about the alias. Then pipe the results to the Format-List cmdlet and choose the * wildcard for the properties. This command is shown here.

PS Alias:\> Get-Item r | fl *

The command and the output associated with the command are shown in the image that follows.

Image of command output

When attempting to understand more about aliases, the crucial things to pay attention to are the Definition and the Options properties.

Note   The command that the alias resolves to is actually available in a number of properties. These properties are ResolvedCommandName, ReferencedCommand, ResolvedCommand, and Definition.

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 Get-ChildItem cmdlet on the Alias: drive and filter on the Definition property for the cmdlet. This technique is shown here (dir is an alias for Get-ChildItem).

PS Alias:\> dir | ? definition -eq ‘get-childitem’

 

CommandType     Name                                               ModuleName

———–     —-                                               ———-

Alias           dir -> Get-ChildItem

Alias           gci -> Get-ChildItem

Alias           ls -> Get-ChildItem

Looking at alias options

Several cmdlets have ReadOnly aliases. These appear when I look at the Options 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 Get-Childitem cmdlet to return all aliases from the Alias: drive. I pipe the resulting AliasInfo objects to the Sort object cmdlet, and I sort on the Options property. I then group the objects by piping to the Group-Object cmdlet. The command and output associated with the command are shown here.

PS Alias:\> dir | sort options | group options

 

Count Name                      Group

—– —-                      —–

    3 None                      {ep, sls, gmh}

   48 AllScope                  {exsn, set, popd, ipsn…}

   99 ReadOnly, AllScope        {rdr, ni, nv, npssc…}

Note   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.

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.

PS Alias:\> gmo -ListAvailable | ipmo

PS Alias:\> dir | sort options | group options

 

Count Name                      Group

—– —-                      —–

   72 None                      {Get-ProvisionedAppxPackage, gfsd, Get-GPPermissi…

   48 AllScope                  {lp, rd, ren, rjb…}

  111 ReadOnly, AllScope        {rcms, rcsn, tee, swmi…}

To delete an alias that is not marked as ReadOnly, all I need to do is use the Remove-Item cmdlet. When I do this, the Get-Item cmdlet informs me that I was successful. These commands are shown here.

PS Alias:\> Remove-Item lp

PS Alias:\> Get-Item lp

Get-Item : Cannot find path ‘Alias:\lp’ because it does not exist.

At line:1 char:1

+ Get-Item lp

+ ~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (Alias:\lp:String) [Get-Item], ItemNo

   tFoundException

    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCom

   Mand

If I attempt to delete a ReadOnly alias, an error arises as shown here.

PS Alias:\> Remove-Item rcms

Remove-Item : Alias was not removed because alias rcms is constant or read-only.

At line:1 char:1

+ Remove-Item rcms

+ ~~~~~~~~~~~~~~~~

    + CategoryInfo          : WriteError: (rcms:String) [Remove-Item], SessionState

   UnauthorizedAccessException

    + FullyQualifiedErrorId : AliasNotRemovable,Microsoft.PowerShell.Commands.Remov

   eItemCommand

However, if I use the –force switched parameter when I attempt to remove the ReadOnly alias, the command succeeds. This is shown here.

PS Alias:\> Remove-Item rcms -Force

PS Alias:\> Get-Alias rcms

Get-Alias : This command cannot find a matching alias because an alias with the

name ‘rcms’ does not exist.

At line:1 char:1

+ Get-Alias rcms

+ ~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (rcms:String) [Get-Alias], ItemNotFou

   ndException

    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.G

   etAliasCommand

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.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.