How to use WMIC to map a network drive?

Posted on 2011-03-25
Last Modified: 2012-05-11
Hi Experts,

I would like to use WMIC to change/add some network drive on a remote windows 7/windows XP workstation. Please advise if this is possible?

1>add a new network share with drive letter P: connected to \\server-name\share and presistent connection

2>edit existing mapped drive share, same drive letter but change the share UNC path, keep presistent connection

many thanks!
Question by:nokyplease
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
LVL 41

Expert Comment

ID: 35220677
No, that's not really practical...   The problem is that mapped drive letters are part of the user's profile.  So, you can't really "get to" the user's profile remotely via WMI.

However, you can accomplish what you're after by editing the user's registry hive.   The changes won't happen immediately, but instead would happen the next time they log in.  Is that a viable option for you?

Author Comment

ID: 35220718
thanks graye, so there is no way to remotely change a user's mapped drive and take instant effect without user's interaction?

if so, please can you teach me how to accomplish this via editing registry?

LVL 15

Expert Comment

ID: 35224284
To make a share remotely you can call this.

wmic, and then press ENTER.
Type /node:computer name where computer name is the name of the target computer. If you want to pass administrator credentials, type /user:"domain\username", to receive a prompt for a password.
Type the following line where c:\test is the path to the folder that you want to share:
share call create "", "this is the comments section", "maximum number of connections", "this is the share name", "", "C:\test", 0

Using DISKPART.exe you can assign a new drive letter. Both from GUI or Console. You can use MMC to edit the drive using WMI if its a domain, works the same.


So having known this you can call WIN32_Process and "Create"\Run a commandline tool to use diskpart to change the drive letter or Create Wrapper for that exe or sending commands to it using SendMessage().

I am not a powershell user, but this should be enough information to get you in the right direction.

Warning:Changing your boot partion drive letter will lock you out!!! be careful.

Even with all of this you would still need to do either 1.) reboot 2.) disconnect & delete + recreate the mapped drive.

Your best bet is to use a batch file on the remote computer and call it with WMI. I really dont think you want to get into any low level programming for this.

@echo off
net use x: /delete
rem remapping drive
net use w: \\newshare

Open in new window

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

LVL 41

Expert Comment

ID: 35224569
Before we go down that path, let's make sure we aren't missing some obvious alternatives.

Mapping of drive letters is a very common thing that is included in a login script.  Most PCs in an domain environment have many login scripts that run via Window's Group Policy Objects (GPOs).  It would be a trivial task to add a few lines of text to an existing login script (or create a new one) to accomplish this common tasks.

Again, these changes would only take place the next time the person logged onto their computer.  Please let us know if you'd like to explore this option or wish to continue down the path of a separate script/program/utility
LVL 10

Expert Comment

ID: 35232241
Here is more information on the share alias.  As noted above, with WMI the create is possible, but, the modification might be a little more challenging.

PS C:\Users\wsteele> wmic share call /?

Method execution operations.

CALL <method name> [<actual paramlist>]
NOTE: <actual paramlist> ::= <actual param> | <actual param>,  <actual paramlist>

The following alias verb(s)/method(s) are available:

Call                    [ In/Out ]Params&type                   Status
====                    =====================                   ======
Create                  [IN ]Access(OBJECT)                     (null)

                        [IN ]Description(STRING)

                        [IN ]MaximumAllowed(UINT32)

                        [IN ]Name(STRING)

                        [IN ]Password(STRING)

                        [IN ]Path(STRING)

                        [IN ]Type(UINT32)

Delete                                                          (null)

SetShareInfo            [IN ]Access(OBJECT)                     (null)

                        [IN ]Description(STRING)

                        [IN ]MaximumAllowed(UINT32)

Open in new window

These are the limitations of the WMIC share alias.  

As far as modifying a specific registry file for another user, this is a little trickier.  There is way to temporarily load another user's registry file and open it without logging into their account using the reg utility.  This uses the reg.exe utlity to load a user.dat file from a path other than the current user.  NOTE: This assumes you do not have roaming profiles.

reg load hku/temp "\\\c$\users\someuser\ntuser.dat"

Open in new window

This loads the registry settings for user someuser into a an HKEY_USERS hive called temp.  You could then work against this profile\hive when using Powershell.  After you are done, be sure to run the following command to release the file.

reg unload hku\temp

Open in new window


Author Comment

ID: 35238882
That wmic share is for remotely setup a network share but not remotely mapping a drive...

I would like to know if there is any method that can remotely map a network share instead of going to user's PC or ask the user map it manually. I know about login script, but this cannot fullfill my requirement that can be take effect instantly. Also cannot meet the situation when the share is just for one time use....

LVL 15

Expert Comment

ID: 35239382
Your first paragraph is not possible the way shares are designed to work. You MUST map a drive as that I how it is shared. Network share is just another name for drive sharing only difference is the context on how you use it.

Just use wmi in a script like a vbs script and use the namespace Win32_Share and create it from there. All you have to do is one thing on there pc. Enable remote WMI and then add the \\domaim\root\Cimv2:Win32_Share in your management scope, connect to the computer and ["Create"] a share.

Example code from MSDN
Const FILE_SHARE = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set objNewShare = objWMIService.Get("Win32_Share")
errReturn = objNewShare.Create _
    ("C:\Finance", "FinanceShare", FILE_SHARE, _
        MAXIMUM_CONNECTIONS, "Public share for the Finance group.")

Open in new window

Just save to text file and save ad a .vbs file extension, change strComputet from "." to the remote computer ip/hostname and this script should work remotely. Last call the file by using cscript yourfile.vbs .
LVL 41

Expert Comment

ID: 35240418
Russell_Venable,  No... he wants the "opposite end" of "Share".  Instead of creating a shared network resource on the remote PC, he wants to create a connection (a mapped drive letter) to an existing shared network resource.

There are several ways to accomplish what you want, but none (that I'm aware of) will meet all of your requirements.

Author Comment

ID: 35241412
Hi graye, yes you see what I mean, I want to remotely setup a drive letter on a pc that maps a network share on a server....

so there is no way to do so? please advice.

many thanks!
LVL 41

Expert Comment

ID: 35242308
Well, there's no way (that I know of) to meet 100% of your requirements.

Yes, you can remotely add a permanent network connection to each remote user's registry hive.  But this change will not go into effect until the next time the user logs in.

Another similar technique is to add an item to the remote PC's All User's Startup menu.  This new item will be a short batch file that performs the mapping (an example of this is in Russell_Venable's orginal post).  But again, it will not go into effect until the user logs out and logs back in.

The 3rd technique (and the simplest of the all), is to edit (or create) a login batch file using Active Directory's Group Policy Object (GPO).  The contents of this batch file is the same as the example referenced above).  But yet again... it won't happen until next login.

Author Comment

ID: 35248742
what if i execute the batch file on the remote pc using wmic or winrs or psexec? will it be mapped with the user or it will mapped to the administrator account which used to execute the command?
LVL 41

Accepted Solution

graye earned 500 total points
ID: 35256662
You have correctly identified the problem....  most of the remote execution tools do not run under the context of the current user.

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question