How to Programmatically List All Shared Folders on Windows Server 2008 Using PowerShell and CMD


2 views

The simplest way to list shared folders is through Command Prompt:

net share

This command displays all active shares with their:

  • Share name
  • Resource path
  • Remark/description
  • Number of connected users

For more detailed information and better automation, use this PowerShell script:

Get-WmiObject -Class Win32_Share | Format-Table Name,Path,Description -AutoSize

To export the results to CSV:

Get-WmiObject -Class Win32_Share | Export-Csv -Path "SharedFolders.csv" -NoTypeInformation

For programmers needing to access this information in applications, here's a C# example:

using System;
using System.Management;

class Program {
    static void Main() {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(
            "SELECT * FROM Win32_Share");
            
        foreach (ManagementObject share in searcher.Get()) {
            Console.WriteLine("Share: {0}", share["Name"]);
            Console.WriteLine("Path: {0}", share["Path"]);
            Console.WriteLine("Description: {0}", share["Description"]);
            Console.WriteLine("----------------------");
        }
    }
}

To view all shares including default administrative shares (C$, ADMIN$ etc.):

wmic share get name,path,type

The type column shows:

  • 0 = Disk drive
  • 1 = Print queue
  • 2 = Device
  • 3 = IPC
  • 2147483648 = Disk drive admin share
  • 2147483649 = Print queue admin share
  • 2147483650 = Device admin share
  • 2147483651 = IPC admin share

Shared folder information is also stored in the registry at:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Shares

To query this from command line:

reg query "HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Shares"

When working with shared folder listings programmatically:

  • Always run elevated (admin) privileges
  • Handle permissions carefully in automation scripts
  • Consider using try-catch blocks for error handling
  • For remote servers, use proper authentication

To check shares on a remote Windows Server 2008 machine:

wmic /node:"RemoteServerName" share get name,path

Or in PowerShell (requires proper permissions):

Get-WmiObject -Class Win32_Share -ComputerName "RemoteServerName"

For Windows Server 2008 administrators needing to audit shared resources, here are the most efficient ways to list all shared folders:

The simplest method that works in all Windows versions:


net share

Sample output:


Share name   Resource                        Remark

ADMIN$       C:\Windows                     Remote Admin
C$           C:\                            Default share
IPC$                                         Remote IPC
DataShare    D:\SharedFiles                 Department Data

For more advanced filtering and processing:


Get-WmiObject -Class Win32_Share | Select-Object Name,Path,Description

This provides the most comprehensive data including security descriptors:


wmic share get Name,Path,Description,Status,Type,AllowMaximum

For environments where PowerShell isn't available:


Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colShares = objWMIService.ExecQuery("Select * From Win32_Share")

For Each objShare in colShares
    WScript.Echo "Name: " & objShare.Name & vbCrLf & _
                 "Path: " & objShare.Path & vbCrLf & _
                 "Description: " & objShare.Description & vbCrLf
Next

For documentation or auditing purposes:


wmic /output:C:\Shares.csv share get Name,Path,Description /format:csv

To detect default admin shares (ending with $):


net share | find "$"

To check shares on a remote server:


wmic /node:"SERVERNAME" share get Name,Path