Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 10 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

So combining the best of the answers here into a one-liner; Tests for current path as UNC, maps a drive to current path, changes to new mapped drive, if already a mapped drive, no action is taken.

IF ((Get-Location | %{$_.Drive}) -eq $NUL){$dltr=%{for($j=67;gdr($d=[char]++$j)2>0){}$d}; New-PSDrive –Name $dltr –PSProvider FileSystem –Root ((Get-Location).ProviderPath) –Persist; Set-Location -Path $dltr`: -PassThru}

Not intended to answer the OP, just add useful content. Had a script that I needed mapped drive instead of UNC paths, so I did this to check if a UNC was being used in PS, then map to a drive using that UNC path, then switch to that drive letter.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .