Search code examples
shellpowershellmappingnetwork-drivenet-use

How do I get disconnected networkdrives in Powershell?


I need to get all mapped networkdrives, even the ones who are currently disconnected. I tried it with Get-PSDrive but that only shows the ones that are currently connected. It is possible get all mapped networkdrives with net use but I can´t reuse the output (I need to save the letter and the path).


Solution

  • You can get the status of network drives from the Users registry using:

    Get-ChildItem hkcu:\network
    

    To access these reg entries you need to use Get-ItemProperty, you can build a PSObject to hold the properties to make this nicer:

    Get-ChildItem hkcu:\network | %{            
        $obj = New-Object PSObject
        $obj | Add-Member Noteproperty Drive $_.name.Replace('HKEY_CURRENT_USER\network\','')
        $obj | Add-Member NoteProperty Path (Get-ItemProperty (Join-Path hkcu:\network $_.name.split('\')[-1]) | Select-Object -ExpandProperty RemotePath)
        [array]$mapped += $obj
    }
    

    Example properties:

    PS> $mapped
    
    Drive Path          
    ----- ----          
    Y     \\server1\share
    Z     \\server2\hidden$