Add a shared network drive mapping.

      objNetwork.MapNetworkDrive(strLocalDrive, strRemoteShare, [persistent], [strUser], [strPassword])

   objNetwork     : A WScript.network object

   strLocalDrive  : The drive letter (e.g. L:)
   strRemoteShare : The UNC path to the remote drive \\MyServer\MyPrinter
                    (String value)
   persistent     : True/False - store the mapping persistently in the users profile
                    default = false
   strUser        :  The user name. (Optional)
   strPassword    :  The password. (Optional)


Dim objNetwork, strRemoteShare
Set objNetwork = WScript.CreateObject("WScript.Network")
strRemoteShare = "\\myserver\users"
objNetwork.MapNetworkDrive "H:", strRemoteShare, False

To run these commands in PowerShell without calling cscript:

$network = New-Object -ComObject WScript.Network
$RemoteShare = '\\myserver\users'
$network.MapNetworkDrive('H:', $RemoteShare, $false)

The example script below takes this a stage further, disconnecting the drive letter first (and coded to cope with persistent and/or disconnected drive shares)

DriveMap.vbs - Connect a local drive letter to a remote file share

For workgroup machines sharing files via pass-thru authentication with no domain login script, a persistent drive map can be used. Persistent drives will automatically connect to the resource whenever it is available.

“The price one pays for pursuing any profession or calling is an intimate knowledge of its ugly side” ~ James Baldwin

Related VBScript commands

NET, list drive mappings - .EnumNetworkDrives
NET, remove drive map - .RemoveNetworkDrive
Equivalent Windows CMD command: NET - Manage network resources
Equivalent PowerShell command: New-PSDrive - Create a mapped network drive.

Copyright © 1999-2024 SS64.com
Some rights reserved