Windows PowerShell


Microsoft PowerShell Drives

Introduction to Microsoft PowerShell's Drives

My biggest surprise with Get-psdrive was that the registry appeared in the results.  I was expecting a list of the local and network drive, but I had not anticipated HKLM or ENV amongst the catalog of drives available to PowerShell.

Topics for Microsoft PowerShell's Drives

Get-psdrive

As anticipated Get-psdrive (note singular noun) displays hard drives such as C:\ and D:\.  Here are the extra namespaces that I was not expecting:

Alias
Cert (Certificates)
Env (Environmental Variables e.g. HomeDrive, UserName
Function
HKLM
HKCU
Variable

To list or investigate the following three 'file systems' you need the get verb, type:

Get-alias
Get-function
Get-variable

However to access the registry (HKLM) or Env you need the 'set' verb

Set-location HKLM: (Note the colon)
Set-location Env:  (Note the colon)

One interesting command is Get-psdrive name.  Where name is HKLM, Variable or Alias.

Childitem (also get-Childitem, or gci)

In the context of a drive, PowerShell uses get-Childitem to list the contents.  You may wish to use dir, or you may prefer to leave the old DOS commands at the PowerShell door and learn the new style verb-noun pairs.  Incidentally, it is worth remembering that all PowerShell nouns are singular, Microsoft are determined to enforce consistency wherever possible.

A handy command is set-location.    Microsoft include a mapping for many old dos commands, therefore if you prefer, use the alias cd, rather than the modern set-location.

MapNetworkDrive

I have to confess that at first I was unable to map a network drive.  However, eventually I hit upon this indirect method:

Success Experiment 1

$net = $(New-Object -ComObject WScript.Network)
$net.MapNetworkDrive("s:", "\\grand\ezine")

(Failed Experiment 1)

1) Mapped a network drive to x:   (\\grand\scripts)
2) Get-psdrive x | format-list
3) Nowhere in the property list could I see reference to (\\grand\scripts) or anything similar

(Failed Experiment 2)

1) I discovered that you could create a new drive so I tried
2) New-psdrive y
3) Provider FileSystem  (PowerShell asked for Provider)
4) Root Y:\  (PowerShell asked for Root)

In PowerShell, the command Drive revealed the new drive, however the letter Y:\ did not appear in Explorer.

Success Experiment 2

1) New-psdrive -name GuyScripts -psprovider filesystem -root d:\ scripts
2) Set-Location GuyScripts:
3) Note the colon with GuyScripts:

Summary of PowerShell's Drives

The Get-psdrive command returned more items than I expected.  In amongst the usual C:\ and D:\ drive letters was HKCU and Alias.  By creating a new com object it's possible to MapNetworkDrive just as you would with VBScript.

  ˚

See Also

Windows PowerShell Home  • Introduction  • Cmdlets  • Exchange 2007  • Profile.ps1  • $_.Pipeline

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

 *


Google

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.