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.
$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")
[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–