Category Archives: Howto Tutorials (EN)

Knowledge Network for Tutorials, Howto’s, Workaround, DevOps Code for Professionals.

Network drive mapping from PowerShell Script

Mapping Windows Network Drive using PowerShell

PowerShell scripts can be used to mapping network drives to Windows network shares on servers or NAS devices. Where batch processing is not the right choice, or where Group Policy is not possible. A PowerShell script can perform this task. For example, when running login scripts with remote clients via VPN. Or clients that are not members of the AD domain.

Network drive mapping from PowerShell Script

PS-Script for Network Drive Mapping

This PowerShell Script example netdrive.ps1, creates the Windows network mapping to drive Z: for a specific user, whereby you can log on as a different user than you are authenticated with on the client.

$LogFile = "netdrive.log"
Get-Date -Format "MM/dd/yyyy HH:mm" | Out-File $LogFile -Force
Write-Host "`nEnter your network credentials to map network drives or cancel mapping" -ForegroundColor Yellow
$Cred = Get-Credential -Message "Please enter your credentials" -UserName "domain\username"
if ($Cred) {
    $sharePath = "\\server\share"
    $mapDrive = "Z"
    Write-Host "`nMapping network share.." -ForegroundColor White
    Add-Content $LogFile -Value "Mapping drive $mapDrive $sharePath"
    New-PSDrive -Name $mapDrive -Root $sharePath -Persist -Scope Global -PSProvider "FileSystem" -Credential $Cred | Out-File $LogFile -Append
} else {
    Add-Content $LogFile -Value "No credentials were given, network mapping canceled."
}

The corresponding Windows network share is defined in line 6 at $sharePath, and the drive is assigned in line 7 at $mapDrive, change the respective placeholder between the quotation marks. If further network shares are to be mapped, in the if loop copy lines 6 – 10 for the next network share using the variables $sharePath1 and $mapDrive1.

Run the script in the PowerShell with change to directory in which you placed the netdrive.ps1 script file.

.\netdrive.ps1

Replace the placeholders domain\username with the appropriate domain and user, if there is no domain, then just leave it blank.

Windows PowerShell Credentials
Windows PowerShell Credentials

Run PowerShell Script from Batch

If a batch is already in place, for example netlogon.bat, then execute netdrive.ps1 out from a batch file as follows:

start /wait "" powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File netdrive.ps1

After a successful logon, is it possible to check the network mapping, for this the processes are logged in the file netdrive.log.

Conclusion

In this Tutorial you will learn, how to using PowerShell scripts that can be used to mapping network drives. To Windows network shares on servers or NAS devices. Where batch processing is not the right choice, or where Group Policy is not possible.

Network drive mapping from VBScript

Mapping Windows Network Drive using Visual Basic Script

VBScript netdrive.vbs for Network drive Mapping

VBScript can be used to connect network drives to Windows shares on servers or NAS devices. In situations where batch processing is not the right choice, or group policy is not the application you want, a Visual Basic Script can meet the requirement, for example, to apply login scripts to VPN Remote Clients who are not members of the AD domain.

Microsoft VBScript contains object classes, methods (functions / procedures) and value structures. Our MapNetworkDrive object is used as a method here. The method or verb in turn manipulates values.

Any object can be used, the object objNetwork is defined here, script developers like to stick to patterns and their variables. A prefix of str indicates a string value, while the obj prefix indicates an object. After WScript has created our objNetwork, it can be edited using the MapNetworkDrive method.

VBScript for Network drive Mapping

The following VBScript netdrive.vbs as an example, assign the network mapping to drive Z: for a specific user; the login can be performed as a different user than the one with whom you have authenticated yourself on the client.

Option Explicit
Dim objNetwork, strDriveLetter, strRemotePath, strUser, strPassword, strProfile, WshShell

' Set credentials & network share to variables.
strDriveLetter = "Z:"
strRemotePath = "\\server\share"
strUser = "domain\username"
strPassword = "topsecret"
strProfile = "false"

' Create a network object (objNetwork) do apply MapNetworkDrive Z:
Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
strProfile, strUser, strPassword

' Open message box, enable remove the apostrophe at the beginning.
' WScript.Echo "Map Network Drive " & strDriveLetter
MsgBox " Explorer launch Network Drive " & strDriveLetter, vbInformation, "Network Drive Mapping"
' Explorer will open the mapped network drive.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "explorer.exe /e," & strDriveLetter, 1, false
WScript.Quit

The corresponding variable assigned line 5 – 9 in which the placeholder is defined between quotation marks.

Assign VBScript values to variables

  1. For strDriveLetter choose a desired network drive letter.
  2. For strRemotePath add the UNC path to the network share.
  3. For strUser add the user name. If this is a member of an AD domain, the domain prefix must be given, using like domain\user.
  4. For strPassword add the users password.

Run VBScript WSH from batch

If a batch is already used, for example netlogon.bat, then from the batch file the VBScript netdrive.vbs is executed as follows:

start /wait "" cscript //Nologo netdrive.vbs
VBScript Network Drive Mapping

If you do not want to output Visual Basic Console Screen during execution, use option /B to start the application without opening a new window.

start /B /wait "" cscript //Nologo netdrive.vbs
cscript msgbox map network drive

After successfully logging in, the network drive for the Windows network share is created and then opened in Explorer.