How Can I Determine Which Drives are Mapped to Network Shares?

How Can I Determine Which Drives are Mapped to Network Shares?

  • Comments 11
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine which drives on a computer are mapped to network shares?

-- CP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CP. OK, you better sit down, because what we’re about to say will shock you: if you want to determine which drives on a computer are mapped to network shares, don’t use the Win32_MappedLogicalDisk class.

Oh, look: you spilled coffee on everything. Didn’t we tell you to sit down? Here, here’s some paper towels.

While you clean up - don’t forget that little puddle by the phone - we’ll explain why. The truth is, you can use the WMI class Win32_MappedLogicalDisk to determine which drives are mapped to network shares. The problem, however, is that while this class will return the drive letters for these drives, it won’t tell you which shares the drives are mapped to. Weird, but true.

And so it’s Win32_LogicalDisk to the rescue. With this class not only can we determine which drives are mapped to network shares, but we can also determine which network shares they map to. And to do that requires nothing more than a script like this:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colDrives = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where DriveType = 4")

For Each objDrive in colDrives
    Wscript.Echo "Drive letter: " & objDrive.DeviceID
    Wscript.Echo "Network path: " & objDrive.ProviderName
Next

Stop us if you’ve heard this one before, but the script starts out by connecting to the WMI service on the local computer. (As usual, you can modify this script to run against a remote computer simply by assigning the name of that machine to the variable strComputer.) We then use this line of code to return a collection of all the mapped network drives:

Set colDrives = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where DriveType = 4")

The key here - as you might have guessed - lies in our Where clause. We’re asking for all instances of the class where the DriveType is equal to 4; needless to say, a DriveType equal to 4 represents a mapped network drive. (For other DriveType values, see the WMI SDK on MSDN.) The query returns a collection of all the mapped drives; we then set up a For Each loop to walk through that collection. For each mapped drive we echo the value of two properties: DeviceID, which returns the drive letter for the drive; and ProviderName, which returns the network share the drive is mapped to.

In other words, we get back information similar to this:

Drive letter: E:
Network path: \\atl-fs-01\public
Drive letter: F:
Network path: \\atl-fs-01\finance
Drive letter: G:
Network path: \\atl-fs-01\users\kenmyer

That’s OK, we understand: as useful as this script is, it still comes as a bit of a shock that we didn’t use the Win32_MappedLogicalDisk class. Listen, this has been kind if rough on you: why don’t you take the rest of the day off? Just tell your boss the Scripting Guys said it was OK.

Legal disclaimer. On the advice of our attorneys (and, trust us, the Scripting Guys need their own attorneys), we did not say it was OK. (But if you want to try it anyway, well, good luck.)

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I'm trying to do this in Javascript and it works when connecting to the Local machine, but not remotely.

    Stuck on this and I have no idea what's going wrong !

  • When using WMI to connect to a remote machine to run a query, there are several things that can possibly go wrong. The first is permission, in general the account you use to connect to the remote machine must be a member of the local admin group. The connectServer method allows you to specify alternate credentials for a remote connection see the MSDN docs http://bit.ly/eNgNlE  In addition, there can be profile issues, firewall issues, and other potential problems. Take a look at the articles in my WMI library to see if the information there will help http://bit.ly/hpaSwy

  • I'm assuming it's not a permissions issue as I can get the free space from C: drive on that same remote machine.

    Though these machines are a part of an AD domain, and I've seen mention of sessions for the mapped drives which I've been trying to puzzle out, but haven't gotten anywhere yet unfortunately.

    Could the sessions thing be what's stopping my script from working ?

  • @Lawrence If the drives are mapped via the session of the interactive user and WMI is not using that session then the drives will not be visible to WMI.

  • Thanks, that sounds like what I'm running up against.

    Looks like I'll have to find a way around that.

    Thanks again.

  • Is there anyway of being able to import that info into a new machine i.e. using the script to export the mapped drives and then import them onto a new machine?

    Thanks in advance

  • What if you want to know about the Currently logged on User of a remote computer?  How do I enumerate THEIR mapped network drive?

  • that should have read:

    Get-WMIObject -query "Select DeviceID,ProviderName From Win32_MappedLogicalDisk"

  • is there a way to us "IF THEN ELSE" logic to check if a drive exists from the result of the "SELECT" query then skip that drive and remove the rest of the drives?

  • Hi,

    Whats the best way to simply output all of the drives to one echo box please? e.g.

    Drive letter: E:  is mapped to \\atl-fs-01\public

    Drive letter: F: is mapped to \\atl-fs-01\finance

    Drive letter: G: is mapped to \\atl-fs-01\users\kenmyer

    <OK Button>

    All on one screen.

    Thanks

  • I've tried a lot of scripts to get this to work, but hit the wall in the end. It appears to be a permission issue. I confirmed that when I used a software from https://www.maliciousfilehunter.com/ and although it can search local, and removable drives it looks like my permissions where not good for network drives.