Using the New-PSDrive Cmdlet
Creating a New Windows PowerShell Drive
The New-PSDrive cmdlet functions similar to the Subst command: it enables you to map a drive (in this case, of course, a Windows PowerShell drive letter) to a path. For example, this command creates a new Windows PowerShell drive (drive X) that’s mapped to the folder C:\Scripts:
As you can see, New-PSDrive takes three parameters:
-name, which is the name to be given the new drive. We called our drive X primarily because that’s easy to type, but you can give the drive any name you want (just stay away from blank spaces and any restricted characters).
-psprovider, which simply indicates the type of drive being created. Because our new drive is part of the file system, we use the FileSystem parameter. For other types of drives (such as registry drives) you’ll need to use other providers. Fortunately, you can get a list of drive providers by calling the Get-PSDrive cmdlet.
-root, which is, in this case, the folder that serves as the drive root. When we use the command Set-Location x, we’ll end up in C:\Scripts, the root of our new drive.
The upshot? Now you can switch to the C:\Scripts folder merely by typing this command:
If that wasn’t enough, keep in mind that you aren’t limited to creating new drives mapped to the file system; instead, you can map drives within any Windows PowerShell namespace. For example, this command maps Windows PowerShell drive Y to the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion key in the registry:
New-PSDrive -name Y -psprovider Registry -root HKCU:\Software\Microsoft\Windows\CurrentVersion
To access that registry key from now on you merely need to type Set-Location y:
Mapped drives last only as long as your current Windows PowerShell session. If you exit PowerShell and then restart it, your mapped drive will no longer be available. To ensure that your new drive survives between Windows PowerShell sessions, create a PSConfiguration folder in your Windows PowerShell profile folder. For example:
C:\Documents and Settings\gstemp\My Documents\PSConfiguration
Note. How are you supposed to know your Windows PowerShell profile folder? Just type this command:
Get-Variable profile | Format-List
In the PSConfiguration folder, create a file named Microsoft.PowerShell_profile.ps1 and add the following command to the file:
The next time you start a Windows PowerShell session drive X will be available to you. (Think of the profile file as being a sort of autoexec.bat file for Windows PowerShell.)