Powershell : PSDrives


PSDrive stands for PowerShell Drives through which you can access the data provided by the PowerShell providers.

You can Learn more about PowerShell Providers on https://geekeefy.wordpress.com/2015/04/06/powershell-providers/

You will come through following types of PowerShell drives:

  1. Windows logical drives on the computer, including drives mapped to network shares, or the File System Drives
  2. Drives exposed by Windows PowerShell providers, such as the Certificate:, Function:, and Alias: drives.
  3. PowerShell Registry providers, HKLM: and HKCU: drives that are exposed by the Windows

To veiw the list of powershell Drives available on you machine you can type

Get-PSDrive

Like in the below image

psd

These are the PowerShell Data providers exposed in a consitent format like our File Sytem.

PSDrives can be used in various ways to access data sources like Active Directory, registry keys and  perform actions over them using powershell scripts and cmdlets.

EXAMPLE : On a machine I need to check a particular registry key very often, and every time I’ve to open registry Editor (Regedit) and drill down to that specifc registry hive (Suppose HKLM) and find the registry key. Using the PSDrive we can make it fast and make registry accessible like any file system Drive.

Here we’ll map the required registry hive location using New-PSDrive in our powershell console. Just type the below comamnd.

New-PSDrive -Name PowershellReg -PSProvider Registry -Root HKLM:\SOFTWARE\Microsoft\PowerShell\3\

To Access the newly created PSDrive ‘PowershellReg’, just by typing in
cd PowrehshellReg:\

Please do not to forget the Colon just after the PSDrive name (‘ : ‘), otherwise you will get an error.

npsd
To get the property of any of the Registry SubKey , type in the below commands.
npsd2

Now, Suppose you want to access any Registry value, let’s suppose Powershell version can be accessed like this .

npsd3

Well, There are Lot more thing that can be achieved using PSDrives, Like you can aslo create new registry keys using the New-Item cmdlet.

npsd

And you can verify the same using the RegEdit.

npsd

NOTE : 

One important point to note here is all your PSDrives are Non-Persistent and they disappear as soon your session is closed.

To make your session Persistent and to last it even if your session is closed, you can map the PSDrive to the Explorer window using a switch ‘ -Persist ‘ in your New-PSDrive Cmdlet like below –

New-PSDrive -Name 'P' -Description 'Powerhsell study Videos' -PSProvider FileSystem 
-Root '\\127.0.0.1\D$\Powershell\Vids' -Persist

New drive will be mapped to your Explorer.

npsd

QUICK TIP : Persist switch will force you to map drives for remote locations, but you can cheat the cmdlet 😉 by using the loopback IP address or your machine name if you want to map a drive locally.

Hope this was helpful, Happy learning ! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s