Accelerate the PowerShell Console with Custom Drives



Summary: Microsoft Scripting Guy Ed Wilson teaches how to accelerate work in the Windows PowerShell console with PSDrives.


Hey, Scripting Guy! QuestionHey, Scripting Guy! I noticed in one of your screen shots that you were running the script from a rather deeply nested path of c:\data\ScriptingGuys\2011\HSG-1-3-11. I remember thinking what a silly path for a PowerShell kind of guy to use. About the only thing that you could have done to make matters worse on yourself would have been to have put a space in Scripting Guys, and maybe chuck the entire thing in your documents folder in your profile. What gives? Is there not a better way to handle paths in Windows PowerShell? Am I limited to chucking everything into a bin folder that I add to my path, or have an unwieldy long path to deal with?

— MC


Hey, Scripting Guy! AnswerHello MC, Microsoft Scripting Guy Ed Wilson here. You know, one of the fun things about traveling with Dr. Scripto is that he is world famous. When I was doing the fashion photographer thing, and we were having our photo shoot, dozens of people came up to watch the proceedings. During our tour of the Castillo De San Marcosk, Carl “Fritz” Bertoch, a Visitor Use Assistant for the U.S. Department of the Interior, came up and wanted his picture taken. Being the laid back person that he is, Dr. Scripto was more than happy to oblige.


MC, just like having your picture taken with Dr. Scripto, sometimes all it takes is to ask. The reason I have such a convoluted path is that it helps with backups (all my data is in a data folder) and it also helps with organizing all the materials for a weeks’ worth of Hey, Scripting Guy! posts (documents, scripts, figures, and various notes). I have written a script that I use to create all my folders for each year.

Dealing with scripts and convoluted paths is not too terribly difficult in Windows 7 because I can drag-and-drop a script to the Windows PowerShell console and run the script unless the Windows PowerShell console is elevated (running with Admin rights). If I have to edit a script, I can drag-and-drop the script onto the Windows PowerShell ISE and that will populate a window so I can run and edit as needed.

When I was working on my Windows PowerShell 2.0 Best Practices book, I used a PS drive to shorten paths and to make it easier to access the scripts for that project. To create a PS drive, I use the New-PSDrive cmdlet. When using the New-PSDrive cmdlet, I have to specify at least three things: the provider name, the root location for the new drive, and a name for the drive. For now, I will use the FileSystem provider because that provider will allow me to access my hard drive. One of the cool things about the New-PSDrive cmdlet is that I can create a “PowerShell drive” that has a root on a file share, a registry hive, a certificate store, or some other location. It all depends on what providers are installed on the system. The command to create a PS Drive named HSG that has the root of C:\data\ScriptingGuys\2011 using the filesystem provider appears here.

New-PSDrive -PSProvider filesystem -Root C:\data\ScriptingGuys\2011 -Name HSG


When this command is finished, it returns an instance of the PSDriveInfo class. This is seen here.

PS C:\> New-PSDrive -PSProvider filesystem -Root C:\data\ScriptingGuys\2010 -Name HSG10 | Get-Member



   TypeName: System.Management.Automation.PSDriveInfo


Name            MemberType     Definition

—-            ———-     ———-

CompareTo       Method         int CompareTo(System.Management.Automation.PSDriveInfo drive), in…

Equals          Method         bool Equals(System.Object obj), bool Equals(System.Management.Aut…

GetHashCode     Method         int GetHashCode()

GetType         Method         type GetType()

ToString        Method         string ToString()

Credential      Property       System.Management.Automation.PSCredential Credential {get;}

CurrentLocation Property       System.String CurrentLocation {get;set;}

Description     Property       System.String Description {get;set;}

Name            Property       System.String Name {get;}

Provider        Property       System.Management.Automation.ProviderInfo Provider {get;}

Root            Property       System.String Root {get;}

Free            ScriptProperty System.Object Free {get=## Ensure that this is a FileSystem drive…

Used            ScriptProperty System.Object Used {get=## Ensure that this is a FileSystem drive…


Because the object returns, the properties display to the Windows PowerShell console every time that a new PS Drive is created. Most of the time, I like seeing the PSDriveInfo object because it provides confirmation that the command succeeded. This is shown in the following figure.


If I decide, such as I did when I was working on my book, to add the PSDrive to my Windows PowerShell profile, I do not want the distraction of a bunch of text cluttering my screen. Therefore, I pipeline the PSDriveInfo object to the Out-Null cmdlet and discard the PSDriveInfo object. Pipelining return objects to the Out-Null cmdlet does not affect the operation of the command. The command to do this appears here.

New-PSDrive -PSProvider filesystem -Root C:\data\ScriptingGuys\2011 -Name HSG | Out-Null


At this point, MC, you are probably asking yourself, “This is all nice to know stuff, but how do I use the newly created PS Drive?” It is as easy to use a PS Drive as it is to use any other drive on your system. In fact, within the Windows PowerShell environment the C and D drives on my computer show up as PS Drives. I use the Get-PSDrive cmdlet to obtain information about PS Drives. The output appearing here shows this technique.

PS C:\> Get-PSDrive | sort provider


Name           Used (GB)     Free (GB) Provider      Root                                CurrentLoc


—-           ———     ——— ——–      —-                                ———-

Alias                                  Alias

Env                                    Environment

HSG                              88.38 FileSystem    C:\data\ScriptingGuys\2011

D                                      FileSystem    D:\

C                  59.13         88.38 FileSystem    C:\

Function                               Function

HKLM                                   Registry      HKEY_LOCAL_MACHINE

HKCU                                   Registry      HKEY_CURRENT_USER

Variable                               Variable

cert                                   Certificate   \

WSMan                                  WSMan


As seen in the following figure I can use the same commands with my new HSG PS Drive that I use with the C or the D drive.


The cd command is an alias for the Set-Location Windows PowerShell cmdlet, and dir is an alias for the Get-ChildItem cmdlet. Navigating PS Drives is such a fundamental task, that there are three aliases (by default) for these cmdlets. I use the Get-Alias cmdlet and the definition parameter to find aliases for cmdlets I use a lot. Here is the command and the associated output.

PS HSG:\> Get-Alias -Definition Get-ChildItem


CommandType     Name                                      Definition

———–     —-                                      ———-

Alias           dir                                       Get-ChildItem

Alias           gci                                       Get-ChildItem

Alias           ls                                        Get-ChildItem



PS HSG:\> Get-Alias -Definition Set-Location


CommandType     Name                                      Definition

———–     —-                                      ———-

Alias           cd                                        Set-Location

Alias           chdir                                     Set-Location

Alias           sl                                        Set-Location





Of course, I do not have to set my location to the HSG PS Drive to obtain a directory listing. I can do it from the C drive as shown here.

PS C:\> dir hsg:



    Directory: C:\data\ScriptingGuys\2011



Mode                LastWriteTime     Length Name

—-                ————-     —— —-

d—-         1/14/2011   2:55 PM            HSG_1_10_11

d—-         1/14/2011   2:55 PM            HSG_1_17_11

<output truncated>


PS C:\>


Two other neglected cmdlets are Push-Location and Pop-Location. Push-Location stores the current location, and Pop-Location returns to the stored location. When I have to change to a particular location to do some work, I frequently like to push my current location onto the stack, do my work, and then pop back to my previous location. This appears here where I use the aliases pushd and popd. When confronted with an alias I may not understand, I use the Get-Alias cmdlet to look up the definition (that is also illustrated).

PS C:\> pushd

PS C:\> sl hsg:

PS HSG:\> popd

PS C:\> Get-Alias pushd


CommandType     Name                                      Definition

———–     —-                                      ———-

Alias           pushd                                     Push-Location



PS C:\> Get-Alias popd


CommandType     Name                                      Definition

———–     —-                                      ———-

Alias           popd                                      Pop-Location



PS C:\>


MC, that is all there is to using the New-PSDrive cmdlet. Stay tuned tomorrow when I answer more burning PowerShell questions on Quick Hits Friday.

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


Ed Wilson, Microsoft Scripting Guy 


Discussion is closed.

Feedback usabilla icon