Categories

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

Mounting a Virtual/Network Drive with PowerShell

There’s multiple reasons why someone would want to mount a virtual or network drive, especially in an enterprise environment where many users don’t understand the concept of UNC paths or when older software doesn’t support long path names or UNC connections. Now there’s two things we need to know, is this a local path, or is it somewhere on the network. Once we know that we can get started.

If the folder you want to mount happens to exist on the same computer, i.e. locally we can use the subst.exe command. Let’s assume we want to mount the administrators photos to the drive letter P: we should run the following in the command prompt:

subst P: "C:\Documents and Settings\Administrator\My Documents\My Pictures"

Do not put a trailing slash at the end of Pictures, subst.exe doesn’t like it and will complain that it can’t find the path. Of course you don’t even need to run this command from the command prompt, you could run it from the Run menu. In general the command to create a virtual drive is

subst [drive1:] [drive2:]path]

where drive1 is the drive letter that you want to create and drive2:path is the file location of the folder you want to map.

Ok, so now we’v created the virtual drive and we’re ready to get rid of it. This procedure isn’t as easy as right clicking on the drive and selecting Disconnect, after all you can’t disconnect if it’s a local folder. You’re going to have to delete the virtual drive using the delete switch /d

subst P: /d

It really can’t get any simplier.

Ok, so now let’s try mapping a UNC drive with PowerShell. It’s just that simple.

1
2
$drive = $(New-Object -Com WScript.Network)
$drive.MapNetworkDrive("w:", "\\ComputerName\Share")

1 comment to Mounting a Virtual/Network Drive with PowerShell

  • When you use the subst command to map a drive letter to a local folder, the mapping disappears when you log off.

    To make it persistent (to survive logins and reboots), you need to modify the registry. Add a string (REG_SZ) value to:

    HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices

    Set the name of the value to the drive letter (e.g. M:), and the data to:

    \??\C:\folder\subfolder

    This PowerShell one-liner will do that for you:

    Set-ItemProperty ‘HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices’ -name “M:” -value ‘\??\C:\folder\subfolder’

    According to some articles on Web, the drive letter should be immediately available to you, but when I’ve tested it, a restart was required to make it available to all users.

    Btw, to mount local folder as PowerShell drive:

    New-PSDrive -name scripts -psprovider FileSystem -root ‘C:\Documents and Settings\username\My Documents\WindowsPowerShell\Scripts’

    cd scripts:

    This also works for UNC path:

    New-PSDrive -name N -psprovider FileSystem -root ‘\\server\share’

    cd N:

    PowerShell drives are visible only in PowerShell session. If you want some virtual drives to be available every time you work in PowerShell, add a command to your profile.

Leave a Reply