9

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.

  • should be on stackoverflow? – spoon16 Sep 5 '09 at 1:49
14

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.

  • 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
  • @firedfly, how would this need to be altered to support local drives (e.g. `C:`)? It would probably need to include the machine's name, but I don't know how one would differentiate between a HDD and CD/DVD drive. – Craig Sep 22 '16 at 14:39
4

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.

3

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
  • 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
2

I realize this is an old question, but I wanted to share another way to accomplish this:

$drive = Get-PSDrive -Name (get-location).Drive.Name
$root = if($drive.DisplayRoot -ne $null){$drive.DisplayRoot} else {$drive.Root}
Join-Path -Path $root -ChildPath $drive.CurrentLocation

Get-PSDrive will pull back all the information about a drive (name, used/free space, provider, root and current location) and passing the Name parameter as the current drive letter (using get-location) allows this to work in multiple scenerios (this will also pull back information on local drives on the machine).

In order to make it work on both local and mapped drives the comparison is done to populate $root with the drive letter or network location. the .Root will send back the drive letter, and the .DisplayRoot will pull back the network path (null if it is a local path, which is the reason for the comparison)

Using Join-Path, then you can pull together the path, which will return a drive letter and current location if it's a local path, network path and current location if it's a mapped drive.

  • Explain how it works? – Adam Apr 7 '17 at 15:29
  • 1
    Added explaination - Thanks for calling that out. – Ryan Apr 7 '17 at 16:37
  • 1
    Just as info if you are working with SharePoint. I have mapped a drive to a SharePoint document library using the New-PSDrive cmdlet. In this case I have the drive letter in Name property, the Root property contains the UNC path, and the DisplayRoot property is empty. – pholpar Mar 11 at 11:01
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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