Scripting a mapped network drive – Batch, VB and Powershell

Scripting has changed. A new player has come into the works and added more functionality to the Microsoft scripting world, PowerShell.

You can map a network drive via script in a few different ways this post will show you how to do it using batch script, VBScript and PowerShell.

Map a network drive Batch script

Batch script is the most basic way to map a network drive with a script and is pretty easy to do.

net use F: \\fileserver\share /Persistant:yes

Now I will explain this the “net use” command it connects a Windows computer to a shared resource or shows information about shared resources. For more information go to your command prompt and type “net use /?” without the talking marks.

The “F:” part is allocating the drive letter, so when you go to My Computer it will be called drive F.

The “\\fileserver\share” part is specifying the full path to the network resource that you want to map, so in this example the hostname is “fileserver” and the shared folder is “share”

“/Persistan:yes” means that it is to stay there after a reboot. If you do not use the “/Persistant:yes” switch you may not have the shared folder there next time you go to access it.

Map a network drive VBScript

Once you move into VBScript you can add a bit more functionality like looking up groups or Organizational Units and then map different drives for users that are members of the different groups. A good example of this is on thinkpond.net. The basic command is:

WSHNetwork.MapNetworkDrive F: \\fileserver\share

“WHSNetwork.MapNetworkDrive” can be broken up into two parts. “WHSNetwork” which is an object for accessing shared network resources and “MapNetworkDrive” which is the command stating you want to map a network drive.

“F:” is specifying the drive letter

The “\\fileserver\share” part is once again specifying the full path to the network resource that you want to map the drive to.

Map a network drive PowerShell

With PowerShell you can add even more functionality than the VBScript within your scripting because of the power in PowerShell (yes thats right power in PowerShell 😉 ) you can do all the things that VBScript can do and more funky stuff like get network credentials automatically like Greg Caporale’s blog outlines.

New-PSDrive –Name “F” –PSProvider FileSystem –Root “\\fileserver\share” –Persist

“New-PSDrive” is a PowerShell cmdlet that is used for mapping network drives(Syntax available here)

“-Name “F”” is the part that specifies the network drive letter

“-PSProvider FileSystem” specifies the provider of the service which in this case is the FileSystem which is saying it is associated with a network share

“-Root “\\fileserver\share”” is saying where the network drive is going to be mapped to

“-Persist” this switch makes the drive persistent so after a reboot it will still be there

If you make a PowerShell script to do this and you want to run it you can run into some problems if you have not enabled PowerShell scripts to be able to run. To find out how to allow PowerShell scripts to run read this post.

Replies: 2 / Share:

You might also like …

Post Comment

Your email address will not be published. Required fields are marked *