7

I need to know, from within Powershell, if the current drive is a mapped drive or not.

Unfortunately, Get-PSDrive is not working "as expected":

PS:24 H:\temp
>get-psdrive  h

Name       Provider      Root      CurrentLocation
----       --------      ----      ---------------
H          FileSystem    H:\          temp

but in MS-Dos "net use" shows that H: is really a mapped network drive:

New connections will be remembered.

Status       Local     Remote                    Network
-------------------------------------------------------------------------------
OK           H:        \\spma1fp1\JARAVJ$        Microsoft Windows Network

The command completed successfully.

What I want to do is to get the root of the drive and show it in the prompt (see: Customizing PowerShell Prompt - Equivalent to CMD's $M$P$_$+$G?)

Community
  • 1
  • 1
JJarava
  • 552
  • 10
  • 18

6 Answers6

10

Use the .NET framework:

PS H:\> $x = new-object system.io.driveinfo("h:\")
PS H:\> $x.drivetype
Network
Jeff Stong
  • 1,506
  • 4
  • 14
  • 26
  • Good tip! And then how do I get the Rootdirectory= Unfortunately RootDirectory points to H:\ again... – JJarava Oct 01 '08 at 16:25
  • @jjarava -- I belatedly realized that although the DriveInfo class will tell you the drive type it won't return the network path. Perhaps one of the other answers can provide that. – Jeff Stong Oct 01 '08 at 16:47
2

A slightly more compact variation on the accepted answer:

[System.IO.DriveInfo]("C")
Goyuix
  • 23,614
  • 14
  • 84
  • 128
1

Try WMI:

Get-WMI -query "Select ProviderName From Win32_LogicalDisk Where DeviceID='H:'"
EBGreen
  • 36,735
  • 12
  • 65
  • 85
1

An alternative way to use WMI:

get-wmiobject Win32_LogicalDisk | ? {$_.deviceid -eq "s:"} | % {$_.providername}

Get all network drives with:

get-wmiobject Win32_LogicalDisk | ? {$_.drivetype -eq 4} | % {$_.providername}

Grant Wagner
  • 25,263
  • 7
  • 54
  • 64
1

The most reliable way is to use WMI

get-wmiobject win32_volume | ? { $_.DriveType -eq 4 } | % { get-psdrive $_.DriveLetter[0] } 

The DriveType is an enum wit hthe following values

0 - Unknown 1 - No Root Directory 2 - Removable Disk 3 - Local Disk 4 - Network Drive 5 - Compact Disk 6 - RAM Disk

Here's a link to a blog post I did on the subject

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • The only "gotcha" with this approach (that seems much cleaner) is that I'm running WINXP, so the win32_volume class is not available! Thanks for the tip anyhow. – JJarava Oct 07 '08 at 11:19
1

Take this a step further as shown below:

([System.IO.DriveInfo]("C")).Drivetype

Note this only works for the the local system. Use WMI for remote computers.

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Jeffery Hicks
  • 301
  • 1
  • 5