Assigning a Network Drive 2

Maintained on

To assign a network drive from the command line, you normally use net use.

However, on its own, the assignment information is lost every time the PC restarts.

If you add the /persistent:yes option, the drive assignment itself survives a restart, but the credentials are not kept, and reconnecting may then fail with an error.

In File Explorer, you can assign the drive and save the credentials at the same time with the following steps.

  • Open File Explorer and choose to map a network drive.
  • Specify the network path and assign the drive.
  • Enter your credentials and click Save credentials.

On this page, we introduce an easy-to-understand way to do the equivalent of the steps above in a batch file.

Source Code

First, the conclusion. You can achieve this with the following code.

cmdkey /add:%server% /user:%user% /pass:%password%
net use O: %directory% /persistent:yes
Variable nameContent
%server%Server name or IP address to connect to (the 〇〇 in \〇〇…)
%user%Credentials (username)
%password%Credentials (password)
%directory%Network path to assign (e.g. \server\share)

By applying the above and running it, both the network drive assignment and the credentials are kept after a restart.

チェック

If you write a password in plain text in a batch file, it can be exposed to anyone who can view the file. Do not place it in a shared folder or anywhere everyone can read it, and manage access permissions carefully.

How to use cmdkey and net use

In the sample above, we used the cmdkey and net use commands to assign a network drive that does not disappear after a restart.

Let’s explain the role of each command.

cmdkey

The cmdkey command adds, lists, and deletes credentials stored in Windows Credential Manager.

When you access a server from File Explorer for the first time, you may be asked to enter your credentials.

The cmdkey command plays the same role as that input.

Options for cmdkey

cmdkey /add:%server% /user:%user% /pass:%password%
OptionContent
/addIP address or computer name
/userAccount name of the destination
/passPassword of the destination

You can check saved credentials with cmdkey /list and delete them with cmdkey /delete:servername.

net use command

The net use command assigns, disconnects, and lists network drives.

net use options

net use %drive_letter% %directory% %password% /user:%user% /savecred /persistent:yes
Variable nameContent
First argumentDrive letter to assign (the part corresponding to C: of the system drive)
Second argumentPath to assign as a network drive (in the form \\server\share)
Third argumentPassword for the destination
/userAccount name for the destination
/savecredSaves the credentials. Not needed when you use cmdkey.
/persistent:yesAutomatically restores the assignment at the next logon and later (specify no to not restore it).

Concrete examples of assigning network drives

From here, we introduce how to assign network drives based on cases you are likely to encounter.

Prompting the user for a username and password

Writing the username and password directly in a batch file is not desirable from a security standpoint.

If you want to prompt for the username and password when the batch file is run, implement it as follows.

@echo off
setlocal

rem Prompt the user for a username and password
set /p user=Enter your username:
set /p password=Enter your password:

rem Add the credentials using the username and password
cmdkey /add:server_name /user:%user% /pass:%password%
net use O: \\server_name\directory /persistent:yes

endlocal

Note that a password entered with set /p is displayed on the screen as is. If you are concerned about people nearby, you can also specify * as the password for net use and enter it at the prompt.

The set command, which receives input from the user, is explained in detail on the following page.

Assigning network drives for multiple IP addresses

If you have multiple servers or NAS devices at home or in the office, you may want to assign several network drives at once.

In that case, you can do it simply by preparing two batch files: the first with the code introduced here, and the second with the code that actually launches it.

Here, we implement the first batch file as network.cmd and the second as start.cmd.

@echo off
setlocal

cmdkey /add:%1 /user:%2 /pass:%3

echo Added credentials for %1

net use %4: %5 /persistent:yes

echo Assigned %5 to drive %4

endlocal
@echo off
setlocal

call network.cmd server_name1 username1 password1 O: \\server_name1\directory1
call network.cmd server_name2 username2 password2 P: \\server_name2\directory2
call network.cmd server_name3 username3 password3 Q: \\server_name3\directory3
call network.cmd server_name4 username4 password4 R: \\server_name4\directory4

endlocal

If you want to know more about the call command, see the following article.

By running the code above, you can assign four network drives at once.

When run, it is displayed as follows.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>start.cmd
Added credentials for server_name1
Assigned directory1 to drive O:
Added credentials for server_name2
Assigned directory2 to drive P:
Added credentials for server_name3
Assigned directory3 to drive Q:
Added credentials for server_name4
Assigned directory4 to drive R:
C:\users\user>

Troubleshooting

System error 1219 is displayed

If you are already connected to the same server with a different username, net use fails with “System error 1219”.

Disconnect the existing connections with the following command, then run it again.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>net use * /delete /y
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

I want to change the credentials

Running cmdkey /add again with the same server name overwrites the saved credentials.

#Command Prompt #Batch Files #Network Drive #net use #cmdkey