Mapping Drive using PowerShell

Suppose we have requirement to Map a drive to a UNC path that user needs to input. How to do it?

Here is my script that can do it for you…

Below script will ask for the User Input and then will map the “K” drive to that path.


On clicking “Ok” it will map. If the drive is already mapped, it will give below error message:




   Function to map the drive
   This script will map the drive to UNC entered by the User.

#Script Name – Drivemapping.ps1
#Creator – Mohd Aslam Ansari
#Date – 23-Dec-15
#Updated – First Version
#References, if any – I have used New-PSDrive function to map the drive.

Function DriveMapping

Param (
      [string] $drive,    
      [string] $UNCLoc

$driveInfo = New-Object System.IO.DriveInfo($drive)
    if (-Not $driveInfo.IsReady)
        New-PSDrive -Name $drive -Root $UNCLoc -Persist -PSProvider FileSystem -scope Global -Verbose
        [Microsoft.VisualBasic.Interaction]::MsgBox("UNC Path ‘"  + $UNCLoc + "’ mapped to " + $drive + " drive successfully.","OKOnly,SystemModal,Information", "Success")
        Write-Output "Drive not ready/available for mapping"
        [Microsoft.VisualBasic.Interaction]::MsgBox("Drive not ready/available for mapping.","OKOnly,SystemModal,Critical", "Error")

[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
[string]$drive = "K"
[string]$UNCLoc = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a UNC path to be Mapped–>", "Path to be Mapped to " + $drive + " drive","")

DriveMapping -drive $drive -UNCLoc $UNCLoc


I used New-PSDrive cmdlet to create it. Please refer to PowerShell help to read more about it.

–End of Article–

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s