Assigning a Network Drive 2

Maintained on

To assign a network drive from the command line, you usually use net use. However, this alone will cause the assignment information to disappear every time the PC is restarted. This can be resolved by using the persistent command, but then the credential information will not be retained.

  • Open Explorer and assign a network drive.
  • Specify the network path and assign the drive.
  • Enter the credential information and click Save Credential Information.

On this page, we will introduce an easy-to-understand method for expressing the above operations in a batch file.

Source Code

First of all, here’s the conclusion.

You can achieve it with the following code.

cmdkey /add:%server1% /user:%username% /pass:%password%
net use O: %directory%
Variable nameContent
%server1%Server name to connect to (the part after \ in \〇〇…)
%username%Credentials (username)
%password%Credentials (password)
%directory%Network path to assign

By applying the above and executing it, both the network drive and credentials will be retained.

How to use cmdkey and net use

In the previous example, we used the cmdkey and net use commands to assign a network drive that won’t disappear even after restarting.

Let’s explain the role of each command.


The cmdkey command is a function for manipulating Windows credentials.

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

The cmdkey command has the same role as this input.

Options for cmdkey

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

net use command

The net use command is a function for operating network drives.

In addition to the allocation used this time, it is possible to disconnect, display a list of allocated drives, and more.

net use options

net use %drive_letter% %directory% %password% /user:%username% /savecred
First argumentDrive letter to assign (corresponding to the C: of the system drive)
Second argumentDirectory path to assign as a network drive
Third argumentPassword for the connection destination
/userAccount name for the connection destination
/savecredIf credentials are required, save the entered account name and password.
/persistentnet use Automatically restore the drive information set by this net use command even after the next logon.

Example of assigning a network drive

Here, we will introduce how to assign a network drive based on actual cases.

Prompting the user for a username and password

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

If you want to prompt the user for a username and password when the batch file is executed, implement it as follows:

@echo off

set /p username=Enter your username:
set /p password=Enter your password:

cmdkey /add:server_name /user:%username% /pass:%password%
net use O: directory


Regarding the command that receives input from the user, it is explained in detail on the following page.

Assigning network drives to multiple IP addresses

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

In such cases, you can simply prepare two batch files, one with the code introduced in this article and the other with the code to actually start the process, to achieve this.

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

@echo off

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

echo Added credentials for %1

net use %4: %5

echo Assigned %5 to drive %4

@echo off

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


If you want to learn more about the call command, please refer to the following article.

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

When executed, it will be displayed as follows:

Command Prompt icon
Command Prompt
Microsoft Windows [Version]
(c) 2024 Ribbit App Development All rights reserved.
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: