How to make SUBST mapping persistent across reboots?

183

72

Does Windows (XP or later) have a built-in way to create persitent drive mappings, like the ones SUBST creates? I found a 3rd party tool psubst. Is there a way to do it without 3rd party tools?

rick

Posted 2009-08-24T22:53:16.790

Reputation: 1 895

It is possible, because psubst is just a .bat file that mucks with the registry. See @davr's answer below for details. – Scott Stafford – 2015-01-29T15:13:28.473

Caveat from the Wikipedia article in the accepted answer: "Starting with Windows Vista, deleted files are immediately deleted permanently, and are not moved to the "Recycle Bin". Prior to Windows Vista (in Windows XP, for example) files from substituted "disks" were moved to the Recycle Bin when deleted." – JMD – 2016-01-27T15:26:46.357

1

@IvoFlipse, that's a good assumption in general but in this case it's just wrong. The '3rd party tool' is a 150 line batch file that provides a nice cli for modifying a registry and cleaning input to comply with subst's picky trailing slash rules, but it's just a pretty wrapper. You can do it without a program, and the README for psubst explains how. Or, see davr's answer below where he quotes the psubst README.

– alexanderbird – 2016-11-21T20:56:11.520

I have added an answer that fixes the Recycle Bin problem – Tony Pulokas – 2019-08-27T01:11:23.230

Answers

156

Well Wikipedia mentions:

C:\>SUBST /?
Associates a path with a drive letter.

SUBST [drive1: [drive2:]path]
SUBST drive1: /D

  drive1:        Specifies a virtual drive to which you want to assign a path.
  [drive2:]path  Specifies a physical drive and path you want to assign to
                 a virtual drive.
  /D             Deletes a substituted (virtual) drive.

Type SUBST with no parameters to display a list of current virtual drives.

So you can associate paths with drive letters using subst. The Persistent SUBST command (psubst) software seems to be darn handy, and they provide a solution to run it from startup:

https://code.google.com/p/psubst/#Inconstancy

Inconstancy

However restart of a system destroys a virtual disk. What to do? A disk can be created after startup. But what to do, when a disk is needed on early steps of a startup? For example, to run services? There is system feature to start a virtual disk from the system registry:

REGEDIT4 

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices] 
"Z:"="\\??\\C:\\Documents and Settings\\All Users\\Shared Documents"

It is enough to create a text file with the extension .REG and run it. When the next starting up of a system, the virtual disk will be exist at logon. It needs to define a name of disk and path. Note that each backslash in the path is doubled.

In Windows, you can run the registry editor as follows:

  1. Start » Run... (or hit Win+R)
  2. Type: regedit
  3. In Windows Vista and above, UAC will pop up, click "Yes".

Ivo Flipse

Posted 2009-08-24T22:53:16.790

Reputation: 24 054

Comment to confirm that the registry solution works in Windows 10. – Jan Doggen – 2015-09-27T12:33:47.000

Is it possible to give the new virtual drive its own name? – Espen – 2015-10-05T12:35:19.637

@IvoFlipse any idea what is the syntax for network map \<MACHINENAME><SHAREDFOLDER> tried "Z:"="\\??\\<MACHINENAME>\\<SHAREDFOLDER>" and "Z:"="\\\\<MACHINENAME>\\<SHAREDFOLDER>" but not working – cilerler – 2016-01-07T16:48:01.753

@cilerler honestly nope, I had to Google the answer to this question as well. I'd say feel free to ask a new question if you can't find the answer here – Ivo Flipse – 2016-01-08T08:32:21.717

is the `?\`` required for local folders? What does it mean? – Devil's Advocate – 2016-05-10T21:29:29.840

4@Devil'sAdvocate, \?? is a virtual directory in the NT object manager namespace. First it checks the logon session DOS 'devices' (really DOS devices are symbolic links to NT devices, usually located in \Device) and then the \Global?? DOS devices. In WinObj you'll find the logon session devices in \Sessions\0\DosDevices\[logon ID]. Notice that logon session devices are all tracked in session 0 (services) for each LSA logon ID. They aren't correlated with desktop sessions. That's why if you map a drive as a limited admin it doesn't exist when elevated; it's a different logon. – Eryk Sun – 2016-07-20T23:35:22.143

2@cilerler, Windows implements UNC paths via the "UNC" DOS Device, so you would use \??\UNC\server\share. The "UNC" device is a link to the native NT device \Device\Mup (multiple UNC provider). Provider devices register with MUP, such as LanmanRedirector (SMB), MailboxRedirectory, RdpDr, and Csc. – Eryk Sun – 2016-07-20T23:44:25.157

This appears to be broken in the latest Windows 10 update. Can anyone else confirm? – Andrew T Finnell – 2016-10-20T20:25:02.230

1@AndrewTFinnell @JanDoggen I can confirm that this works in Windows 10.14394.447. For protection against drive letter changes, I used device paths for the hard-drive targets. Here are the exact exports from regedit: "D:"="\\Device\\HarddiskVolume2\\Users\\glenn\\Alias\\D" "E:"="\\Device\\HarddiskVolume2\\Users\\glenn\\Alias\\E". If manually editing, swap \\ to \. With this setup, I then use directory junctions to place selected folders on this single-drive machine into the appropriate 'Alias' folder such that the paths exactly mirror those on my multi-drive desktop machine. – Glenn Slayden – 2016-12-01T07:49:23.297

I just edit the registry with regedit and create an entry like described here, and it works. However, I now wanted to move the actual location from one drive to another, so I edited the path in the registry: "??\D:\Folder" to "??\C:\Folder". However, even after several reboots, the virtual drive is still mapped to the old destination. Do I need to do something special to either delete the old virtual drive, or edit it? – Svend Hansen – 2017-10-02T09:52:13.710

Glenn, this is a bit late, but if you want to be properly paranoid you might want to use ?\Volume{GUID} instead, since Windows can reorder numbered Volumes on occasion. (Mainly for removable drives, but it disks are swapped out some will inevitably change.) {GUID} will even persist across raw cloning, though remade partitions will get new GUIDs. – SilverbackNet – 2018-02-24T03:58:03.413

@AndrewTFinnell No, it works fine. However, note that every major Windows 10 update resets the configuration in registry - so you need to configure it again. It's a bit annoying, but understandable. – Luaan – 2018-05-19T10:38:58.357

1Good advice to use the GUID. The next question is what's the easiest way to find the guid. The answer is mountvol.exe. – Jamie – 2018-10-22T23:17:18.510

6Nice to know, even though I don't use subst anymore. But back in the times when I did I used to simply have a batch file sitting in my Startup folder which did those things. – Joey – 2009-08-25T08:46:40.530

2This is awesome. (It requires a reboot, FYI.) Our source tree has to start from a specific drive letter so that our PDBs all always align on different computers. This registry setting solves an annoyance that I have had for years with a subst disk not being there with runas which is a big annoyance if you need to (rarely) run Visual Studio under an admin account. – Brian Reiter – 2009-09-23T18:58:00.420

3Joey: That solution doesn't seem to work under Windows 7. When running an application with Administrative privileges, the substituted drive is not visible to the application. – Dave Jarvis – 2012-07-29T19:06:16.023

31Thanks for the solution. FYI, if you type the variable into RegEdit interactively, do not double the backslashes. That is, create a new string variable named "Z:" (without the quotes) and set the value to the string shown above except with single backslashes. – Crispy – 2012-08-21T20:58:46.467

80

Installing it at HKEY_CURRENT_USER has the benefit of having different setups for each user. I prefer to stick the user space setup as long as a system service does not depend on the device.

REGEDIT4
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]
"F Drive"="subst F: D:\\mount\\db"
"G Drive"="subst G: D:\\mount\\log"

Source: http://networkadminkb.com/KB/a446/how-to-use-drive-letters-mount-points-the-same-disk-drive.aspx

166_MMX

Posted 2009-08-24T22:53:16.790

Reputation: 921

1+1 for showing a very elegant way to automagically do it without a batch in the startup folder – dkellner – 2015-08-03T13:34:10.830

3Can also do this from the command line (since XP/Windows 2000 server): reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v "F Drive" /d "subst F: D:\x\db"

NB \ only needs escaping here if it is the last character of the subst command, e.g. when the target is the root directory: "subst F: D:\\"; a single \ there would be interpreted as escaping the " following it. – stevek_mcc – 2016-10-12T10:50:24.303

1This is a better solution. Here's a PowerShell command to make that registry key:

Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "G Drive" -Value "subst G: D:\\mount\\log" – Poulad – 2019-11-04T15:47:04.303

8+1 For showing how to do multiple mappings in one reg file. :-) – megaflop – 2012-05-15T15:37:45.923

5+1 for the ease of this & also explaining why HKEY_CURRENT_USER. :) – CAD bloke – 2013-02-18T08:19:20.037

46

There's an answer in the page you linked to. You don't need the third party tool, you can do it with a simple registry entry, as detailed on that page.

Create a text file named 'mapdrive.reg' with these contents:

REGEDIT4 

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices] 
"Z:"="\\??\\C:\\Documents and Settings\\All Users\\Shared Documents" 

Then should just be able to double-click on it to set up, no 3rd party tools needed.

davr

Posted 2009-08-24T22:53:16.790

Reputation: 4 809

9If doing this in regedit, make sure to use single backslashes. e.g. ??\C:\Some\Directory – Droj – 2016-07-28T12:19:50.597

8the advantage to the reg key versus the batch file is that it sets the subst drive before any other startup commands run, in case some of those need the subst drive to function – davr – 2009-08-24T23:37:07.733

6@Ivo: I'd call it a healthy fear of the unknown ;) – Isxek – 2009-08-24T23:55:54.590

28

I found this because I was looking for an improvement over my startup subst scripts - they worked OK, but sometimes executed after folders crashed on startup because my drive wasn't yet mapped.

First, I edited the registry, but was unable to get it to work. However, I did come across this alternative, which although it doesn't use subst does answer for me the question, "How to make SUBST mapping persistent across reboots?" Don't use subst... (insert appropriate 'computerName' and 'pathName'):

net use u: "\\computerName\c$\pathName" /persistent:yes

I adapted this from Hank Arnold's suggestion at this discussion of mapping local drives. I had guessed that \\myComputerName\c might work, but I didn't know to apply the $.

Before you try the "net use" command, try to navigate to

\\computerName\c$
to make sure you have 'computerName' correct.

sage

Posted 2009-08-24T22:53:16.790

Reputation: 873

1It is visible to the file system, and it is visible to the local file system -- it's marked as a network drive, just as some drives are marked as removeable drives. That doesn't make such drives not a part of, or not visible to, the local file system -- it makes them network/removable drives in the local file system. – user165568 – 2015-01-02T09:35:57.697

1In testing I did for a performance issue (a couple of years back now) using the (locally) mounted network drive is also slower than using the file system (or subst etc.) directly.
This is not a tremendous difference for small files or occasional use -- not like going over the actual network to another 'server' -- but it is significant.
– HerbM – 2017-01-08T13:47:48.807

2Excellent solution, albeit with two problems: 1) it's not visible to the local file-system, 2) permissions... – ashes999 – 2011-10-11T15:41:55.687

6if you use \\localhost\c$\path\to\thing it is portable across machines (or upgrades that change the computer name). A drawback of using a mapped drive on Win7+ is that different security policies apply to network drives than local, and some programs will just refuse to run off a mapped drive. – matt wilkie – 2013-05-28T06:55:31.527

8

Drop a batch file in your Startup folder that does all the SUBSTs that you want to do.

EBGreen

Posted 2009-08-24T22:53:16.790

Reputation: 7 834

5

Since the original PSubst has some limitations and seems to be concluded by the author, I had to code a new version for my needs, fixing some problems and adding functionality and flexibility.

Check here for PSubst version 3 batch file

https://github.com/cyberponk/psubst

Compatible with Windows 7, Windows 8, Windows 10, Windows Server 2012

Note: This work has no intention of demeriting the original author and only came to existence because of real necessity, as follows:

  • As a newtork admin I have the constant need for SUBST´ing network drives and fiddling with different network users.

  • As not everyone is a Windows expert, i figured the automatic admin elevation with error handling would help, based on coments I read through the web.

  • As some people struggled with the arguments order, I figured it would be best for the script to work with arguments in any order.

I can´t recall of every added functionality, but every inclusion was done purely by necessity. I am sharing the code so that others may not encounter the troubles I had. By this there is no intention other than sharing and helping.

cyberponk

Posted 2009-08-24T22:53:16.790

Reputation: 171

Please, saying "A" say "B". You've siac that the original psubst has a lot of problem - please point on them if they are.

On the other hand, psubst is not abandoned. It's just achieved the limit of its development. The original tool follows very simple philosophy: do something and do well. It is not overloaded with extra functionality like handling with UAC and creating additional vbs-code for doing something more.

One thing can be done over the original code - some cosmetic changes to simplifing the code and improve readability. – jsxt – 2016-02-29T06:33:40.997

Editted, please review. UAC and vbs is for automatic admin elevation. I disagree on the "limit of development" comment, as all changes I made were to make the use even more simple and easy after I encountered several problems using PSUBST. I do not intend to criticise the original author, so I will not list problems, but instead I created solutions. I actually proposed to add my editions in the original github branch, but got refused, so I had to create my own branch. – cyberponk – 2016-03-01T16:56:22.777

Thank you for your fast and detailed feedback. Most probably I need have a look to your development closer. – jsxt – 2016-03-01T22:20:32.263

My hat's off to you; I've spent my time in the batch scripting trenches, and I know how painful it is. That code looks quite good. – SilverbackNet – 2017-06-14T19:01:42.733

2

I am expanding on davr's answer because any files that are deleted from the mapped drive are permanently deleted instead of going to the Recycle Bin. This REG file is based on the information compiled from this TechNet article, which discusses how to enable the Recycle Bin. The example maps the virutal Q drive to folder "OneDrive - Test", and enables the Recycle Bin on the Q drive. Change the paths and name to suit your situation.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices]
"Q:"="\\??\\C:\\Users\\Tony\\OneDrive - Test"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions\{9147E464-33A6-48E2-A3C9-361EFD417DEF}]
"RelativePath"="Q:\\"
"Category"=dword:00000004
"Name"="Q_Mapped_OneDrive"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\KnownFolder\{9147E464-33A6-48E2-A3C9-361EFD417DEF}]
"MaxCapacity"=dword:0000c7eb
"NukeOnDelete"=dword:00000000

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions\{9147E464-33A6-48E2-A3C9-361EFD417DEF}]
"RelativePath"="Q:\\"
"Category"=dword:00000004
"Name"="Q_Mapped_OneDrive"

[HKEY_CURRENT_USER\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\KnownFolder\{9147E464-33A6-48E2-A3C9-361EFD417DEF}]
"MaxCapacity"=dword:0000c7eb
"NukeOnDelete"=dword:00000000

If you only have one such mapped drive, then you can get away with the GUID {9147E464-33A6-48E2-A3C9-361EFD417DEF}. If you have multiple mapped drives, then each should be matched to a GUID from your favorite GUID generator.

Tony Pulokas

Posted 2009-08-24T22:53:16.790

Reputation: 141

Some of my observations: if you just set the values under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions\{GUID} and reboot, the corresponding values under HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\KnownFolder\{GUID} are auto-generated by Windows. Nothing showed up for me under the Wow6432Node keys, but the recycle bin worked without them. This was on a Windows 7 system, so I'm not sure if it behaves differently on newer versions. – Herohtar – 2019-09-10T21:04:00.853

1Just to add a bit of data, I have a 64-bit Win 10 system, and I tested what happened when deleting from a 32-bit app (FastStone Image Viewer), and I found that the recycle bin did not work for the 32-bit app without the Wow6432Node keys. – Tony Pulokas – 2019-09-11T19:40:17.657

1

Here are two examples using reg.exe to add the required entry to the Run Registry subkey of the HKEY_CURRENT_USER hive. These examples can be adapted as required. Open an unelevated cmd window as the current user, and paste in the adapted line.

The simplest use-case:

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v "Map I" /t REG_SZ /d "subst I: \"C:\path\to\folder\"" /f

Where SUBST's path argument has an environment variable embedded in it:

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v "Map J" /t REG_EXPAND_SZ /d "subst J: \"^%DROPBOX_HOME^%\Notes\"" /f

Note that there are two lots of character escaping in the second example. Backslashes for the inner double-quotes that surround the path argument, and carets for the environment variable's percentage signs. Double-quotes aren't always required, but to avoid problems with folder names containing spaces it's recommended to use them.

Jimadine

Posted 2009-08-24T22:53:16.790

Reputation: 832