#PSTip Enumerating mapped network drives using WScript.Network

In the ‘Create mapped network drive using WScript.Network‘ tip, it was shown how a mapped drive can be mounted. The WScript.Network object also contains the EnumNetworkDrives method which enumerates all locally mapped network drives:

(New-Object -ComObject WScript.Network).EnumNetworkDrives()

The output of this method is unfortunately a bit disappointing. It returns a COM object containing an array of strings:

To make this information more useful the following function can be used:

Function Get-MappedDrive {
    (New-Object -ComObject WScript.Network).EnumNetworkDrives() | ForEach-Object -Begin {
        $CreateObject = $false
    } -Process {
       if ($CreateObject) {
           $HashProps.NetworkPath = $_
           New-Object -TypeName PSCustomObject -Property $HashProps
           $CreateObject = $false
       } else {
           $HashProps = @{
               LocalPath = $_
           }
           $CreateObject =$true
       }
   }
}

This function gathers the output of the EnumNetworkDrives method and organizes this into PowerShell objects:

This function is also available in the TechNet Script Gallery available here.

Share on: