I found a solution to permanently mount a drive. However the drive is not mounted on every session. I did this on Windows Server 2019.
Let's say we want to mount a directory called target_dir
located on target_server
.
My username is my_username
and my password my_password
on a domain called MY_DOMAIN
.
Let's mount it on the T:
drive.
Firstly, create a new text file anywhere (on your Desktop for instance). Open it and parse this:
@ECHO OFF
net use s: \\target_server\target_dir /persistent:yes
Then, rename it to mount_drive.bat
and move this file to C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup
.
Be sure that the file is not a .txt
file anymore.
Now, press Win+R
, type gpedit.msc
and run the Local Group Policy Editor.
Browse to Computer Configuration\Windows Settings\Scripts (Startup/Shutdown)
and double-click on Startup
.
Click on Add...
then Browse...
, select the file you have just moved.
Then browse to Computer Configuration\Administrative Templates\System\Logon
, set Always wait for the network at computer startup and logon
to enabled
, save and close.
Open an elevated terminal to save your credentials. Then configure Windows in order not to disconnect this drive after a timeout.
cmdkey /add:target_server /user:MY_DOMAIN\my_username /pass:my_password
NET CONFIG SERVER /AUTODISCONNECT:-1
You can optionally verify everything works by running C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup\mount_drive.bat
Reboot