Saturday, 26 September 2009

A problem that I often have is trying to get the UNC path for a mapped drive so that I can pass it to somebody or that they can pass the UNC path to me. My ideal solution would be to execute a command passing the drive letter as an argument and having the UNC path copied to the clipboard so that I can then paste the UNC path in an email or instant message, so I wrote the following script using PowerShell and the PowerShell community extensions (PSCX).

If you don't already have them installed install the following:

Then save my script to a folder on your computer and run it as follows

.\DrivePath z

Where 'z' is the drive letter for the mapped drive, the UNC path will then be on your clipboard and you can then paste it wherever you like.

    [Parameter(position=0, Mandatory=$true)]
    [string] $Drive = "Z:"

# ensure the supplied param ends with ':' 
$Drive = $Drive.ToUpper().ToCharArray()[0] + ":"

$logicalDisk = Gwmi Win32_LogicalDisk -filter "DriveType = 4 AND DeviceID = '$Drive'"

out-clipboard $logicalDisk.ProviderName


Download script: