3

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).

Manu
  • 83
  • 6

2 Answers2

3

while the HKCU path solution posted by James C. is likely a better solution, you CAN get the output of net use into objects. lookee ...

net use |
    Select-Object -SkipLast 2 |
    Select-Object -Skip 6 |
    ForEach-Object {$_ -replace '\s{2,}', ', '} |
    ConvertFrom-Csv -Header Status, DriveLetter, UNC_Path, Network |
    Select-Object -Property DriveLetter, UNC_Path

hope that helps,
lee

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
  • 2
    Nice one +1, easier: `(net use) -replace '\s{2,}', ',' |sls '\\\\'|ConvertFrom-Csv -Header Status, DriveLetter, UNC_Path, Network` –  Oct 11 '18 at 12:10
  • @LotPings - i keep forgetting about Select-String. matching on the double slash is likely faster, too! [*grin*] – Lee_Dailey Oct 12 '18 at 06:02
2

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$
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
  • Thanks for the answer but I don´t really get what this code does... So how do I acces just the "Y" or just the "\\server2\hidden$" for example? Is there a way to iterate through it? (I am really new to Powershell) – Manu Oct 11 '18 at 14:00
  • 1
    Yes you can iterate through the object, here's a blog on the subject: https://www.itprotoday.com/powershell/iterating-through-collections-powershells-foreach-loops – henrycarteruk Oct 11 '18 at 14:24