#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:

1To 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:

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

About the author: Jaap Brasser

Jaap is a Senior System Engineer in the Financial Services industry. He focuses on Microsoft and related technologies and has a passion for PowerShell. In his spare time he maintains his Blog, Twitter feed, @Jaap_Brasser, and contributes on the Official Scripting Guys Forum and Windows PowerShell TechNet forums

Related Posts

%d bloggers like this: