Using PS Drive
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Upgrade your plan to get instant access to this and many more premium courses. Click the Upgrade Plan button below to get started.
In this lecture we will learn how to use PSDrive. The PSDrive cmdlet allows you to view, create and remove PowerShell drives. A PSDrive is considered a data store location that can represent the file system, a registry hive and network share, among other things as well.
If you want to follow along, download the lesson that came with this lecture. You'll find all the commands that we're going to use in the lesson.
- Go ahead and open PowerShell as the current user. We will use the cmdlet New-PSDrive to create a temporary or persistent drive that is mapped to or associated with a location in a data store.
To see the syntax of PSDrive type Get-Help New-PSdrive, press return.
- We will be using the command New-PS Drive and the parameters Name, PSProvider and Root.
- In our example, daily on a machine, you need to check a particular registry key. You have to open the registry editor, and drill down to that specific registry hive. In this case HKLM, and find the registry key.
I'm going to show you how to use New-PS drive to map that registry location to a name. And make it assessable like any file system drive.
- Now go ahead and copy and paste the first command into PowerShell.
New-PSDrive -Name PSReg -PSProvider Registry -Root HKLM:\SOFTWARE\Microsoft\PowerShell\3\
Now what are the following parameters used for? -Name specifies a name for the new drive -PSProvider this shows that the drive is associated with the registry. -Root specifies the data store location to which a PowerShell drive is mapped. And HKLM is the target registry hive. Now press return.
To access the newly created PSDrive Type cd space psreg: don't forget the colon just after the psreg: name, otherwise you will get an error. And press return.
Now type dir, and press return. And there is our registry entries.
- Additional PSDrive cmdlets. Get-PSDrive. This command that gets the drive in the current session. New-PSDrive creates a PowerShell drive that's mapped to a location in a data store, such as a network drive, a directory on the local computer, or a registry key.
Remove-PSDrive. This cmdlet deletes our PowerShell drives that were created by using the New-PSDrive cmdlet.
Now we'll create a PSDrive and map it to a local folder. Now will copy and paste the second command into PowerShell.
New-PSDrive -Name "Win" -PSProvider "FileSystem" -Root “C:\windows\media”
Now for parameters, -Name specifies the name for the new drive. In this case it will be win. The -PSprovider parameter, it'll be file system and the -root will be the folder C:\ windows\ media. Now go ahead and press return.
To access your new PSDrive, type CD space win: then type dir.
- One important point to note here is that all your PSdrives are non-persistent and that means they'll disappear as soon as your session is closed.
To make your PSDrive persistent and last even if your session is closed, you can map the PSDrive to appear in Windows Explorer using the switch -persist.
- Now copy and paste the third command into PowerShell.
New-PSDrive -Name 'L' -PSProvider FileSystem -Root '\\127.0.0.1\C$\Recycler -persist
Server Academy Members Only
Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..
I could not get command 3 to work on windows 10 or on windows server2016 it kept giving me this error.
PS C:\Windows\system32> New-PSDrive -Name ‘L’ -PSProvider FileSystem -Root ‘\\127.0.0.1\C$\Recycler’ -persist
New-PSDrive : The specified network resource or device is no longer available
At line:1 char:1
+ New-PSDrive -Name ‘L’ -PSProvider FileSystem -Root ‘\\127.0.0.1\C$\Re …
+ CategoryInfo : InvalidOperation: (L:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
Hi Ambrose Garza
I get the same on Windows 10 and Windows Server 2016. It seems the Recycler folder is not available under C:\ drive unless you create one folder with the same name.
Im on windows 11 and have the same issue of command #3 resulting in nothing but errors… Maybe lesson needs some updating
We will be updating.