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
Sign up to access the rest of this lesson
You must either log in or sign up to access this lesson.