Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Assuming I have a mapped drive M:\ to \\SomeServer\SomeShare and in powershell I'm in the folder M:\SomeFolder how do I convert that to a UNC path, i.e. \\SomeServer\SomeShare\SomeFolder.

share|improve this question
    
should be on stackoverflow? –  spoon16 Sep 5 '09 at 1:49

3 Answers 3

up vote 5 down vote accepted

I'm fairly new to PowerShell, so the code below may be poor quality. However, it should get the information you want:

$currentDirectory = Get-Location
$currentDrive = Split-Path -qualifier $currentDirectory.Path
$logicalDisk = Gwmi Win32_LogicalDisk -filter "DriveType = 4 AND DeviceID = '$currentDrive'"
$uncPath = $currentDirectory.Path.Replace($currentDrive, $logicalDisk.ProviderName)

$uncPath should contain the UNC path that you are looking for.

share|improve this answer
    
Thanks very much, that works perfectly. I've modified it slightly to work with local paths as well, this should fix my annoying problems with StExBar + runas + powershell to a mapped drive. –  Chris Chilvers Aug 26 '09 at 13:26

For any one interested in the RunAs script for StExBar it is:

param([string] $username)

$path = Get-Location
$currentDrive = Split-Path -qualifier $path
$logicalDisk = Get-WmiObject Win32_LogicalDisk -filter "DeviceID = '$currentDrive'"

if ($logicalDisk.DriveType -eq 4)
{
    $path = Join-Path $logicalDisk.ProviderName (Split-Path -NoQualifier $path)
}

$systemroot = [System.Environment]::SystemDirectory

&"$systemroot\runas.exe" /user:$username "$systemroot\windowspowershell\v1.0\powershell.exe -NoExit -Command \`" &{ Set-Location '$path' }\`""

And the command line in StExBar is:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -Command "&{ &'%homedrive%%homepath%\RunAs.ps1' 'domain\username' }"

Replace the path with where ever you keep the RunAs.ps1 script, I like to store mine in the root of my home folder.

share|improve this answer

I know this is an old question, but in case someone needs the one line version of this:

$unc = (gwmi Win32_LogicalDisk -filter "DeviceID = '$((Get-Location).Drive.Name):'").ProviderName
share|improve this answer
    
Note this only works if you actually are on a drive-mapped network path. If you're on a local path, ProviderName is null; if you're on a non-drive-mapped network path, Drive is null. If you need to distinguish between these 3 cases, a one-liner won't cut it. –  Timbo Aug 30 '13 at 18:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.