none
Extract UNC path from network drive path RRS feed

  • Question

  • Given a network mapped drive, and a folder "on" that drive, is there any way to extract the UNC path to the folder? So, if P:\ = \\Server\Projects and I provide P:\2015\House I should get back \\Server\Projects\2015\House. 

    I have been playing with Get-Item, but none of the properties are the UNC path, and my Google Fu is failing me.

    Thanks!

    Tuesday, September 30, 2014 11:21 PM

Answers

  • Hi,

    Not sure you are seeking for string replacement like below:

    'P:\2015\House'.Replace('P:', '\\Server\Projects')

    If you are looking for the mapped drives and using Get-Item or Get-ChildItem, Pls try the below example:

    #To collect mapped drives and it's path.
    $MappedDrives = @{}
    Get-WmiObject win32_logicaldisk -Filter 'drivetype=4' | Foreach { $MappedDrives.($_.deviceID) = $_.ProviderName }
    
    #The below samples add additional property called 'UncPath' to the output of Get-item and Get-ChildItem
    Get-ChildItem P: | Select *, @{n='UncPath'; e= { $_.FullName.Replace( $_.Root, $MappedDrives.($_.Root.ToString().SubString(0,2)) +'\') }}
    Get-Item P:\2015\House | Select *, @{n='UncPath'; e= { $_.FullName.Replace( $_.Root, $MappedDrives.($_.Root.ToString().SubString(0,2)) +'\') }}
    

    rgds,

    AZ

    Wednesday, October 1, 2014 3:10 AM

All replies

  • Hi,

    Give this a try:

    PS C:\> $path = 'Z:\2015\House'
    
    $drive = (Split-Path $path -Qualifier).Replace(':','')
    $leaf = Split-Path $path -NoQualifier
    
    $unc = Join-Path (Get-PSDrive $drive).DisplayRoot -ChildPath $leaf
    
    $unc
    
    Example output:
    \\serverName\music\2015\House



    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, September 30, 2014 11:42 PM
    Moderator
  • That's failing for me on the Join-Path, but I also just heard from my customer that they have very weird network security, and I am not going to be able to access the network anyway. So I need to look at some other solution. None of them really graceful. Ah well. 

    Thanks!

    Gordon

    Wednesday, October 1, 2014 12:20 AM
  • Hi,

    Not sure you are seeking for string replacement like below:

    'P:\2015\House'.Replace('P:', '\\Server\Projects')

    If you are looking for the mapped drives and using Get-Item or Get-ChildItem, Pls try the below example:

    #To collect mapped drives and it's path.
    $MappedDrives = @{}
    Get-WmiObject win32_logicaldisk -Filter 'drivetype=4' | Foreach { $MappedDrives.($_.deviceID) = $_.ProviderName }
    
    #The below samples add additional property called 'UncPath' to the output of Get-item and Get-ChildItem
    Get-ChildItem P: | Select *, @{n='UncPath'; e= { $_.FullName.Replace( $_.Root, $MappedDrives.($_.Root.ToString().SubString(0,2)) +'\') }}
    Get-Item P:\2015\House | Select *, @{n='UncPath'; e= { $_.FullName.Replace( $_.Root, $MappedDrives.($_.Root.ToString().SubString(0,2)) +'\') }}
    

    rgds,

    AZ

    Wednesday, October 1, 2014 3:10 AM
  • have you tried this:

    Get-WmiObject Win32_NetworkConnection  | ft "RemoteName","LocalName" -A


    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.
    IT Stuff Quick Bytes

    Wednesday, October 1, 2014 8:35 AM
  • AZ, that Get-Item approach did the trick! Thanks!
    Gordon
    Wednesday, October 1, 2014 10:43 AM