The question: How to get UNC path of network mapped drive? If you are looking for the UNC path of a network drive use Get-PSDrive
. It will gather all of the information about a drive. This includes the provider, root, current location, used space, free space, and name.
$drive = Get-PSDrive -Name (get-location).Drive.Name
Let us start this, for instance the name
parameter is used as the current drive letter by using Get-Location
.
$root = if($drive.DisplayRoot -ne $null){$drive.DisplayRoot} else {$drive.Root}
Next, we continue by comparing the .Root
data with the drive letter and network location. .DisplayRoot
gives us the network location / network path. .Root
gives us the drive letter. It will be Null
if it is a local path as there is no .DisplayRoot
to provide.
$V = Join-Path -Path $root -ChildPath $drive.CurrentLocation
Test-Path -Path $V
Lastly using Join-Path
put it all together to give the network path and current location if it is a mapped drive. If not you will get just the drive letter.
In conclusion we were able to get the full path of a network and mapped drive with just a few lines of code. This is incredibly useful when running reports for mappings, or if you are working on consolidating the drive maps in your organization.
If you enjoyed this check out some of our other posts.