When administering Windows servers (2000/2003/2008), administrators often need to audit shared folders and their corresponding physical paths. While you can view shares through Computer Management, programmatic access provides more flexibility for scripting and automation.
The simplest way to retrieve this information is through WMIC (Windows Management Instrumentation Command-line):
wmic share get name,path
This returns output like:
Name Path ADMIN$ C:\Windows C$ C:\ IPC$ Shared D:\CompanyData
For more advanced filtering and processing, PowerShell provides better options:
Get-WmiObject -Class Win32_Share | Select-Object Name,Path,Description
To export to CSV:
Get-WmiObject -Class Win32_Share | Where-Object { $_.Path -ne $null } | Select-Object Name,Path | Export-Csv -Path "SharesReport.csv" -NoTypeInformation
For integration with other applications, here's a C# example:
using System; using System.Management; class ShareEnumerator { 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}\n", share["Path"]); } } }
To exclude administrative shares (like C$, ADMIN$):
Get-WmiObject -Class Win32_Share | Where-Object { $_.Path -ne $null -and $_.Name -notlike '*$' } | Format-Table -AutoSize
Remember that accessing share information might require administrative privileges. When scripting:
- Always run with elevated privileges
- Handle exceptions for permission denied cases
- Consider using try-catch blocks in your code
If you encounter "RPC server unavailable" errors:
- Verify the Server service is running (net start lanmanserver)
- Check firewall settings for WMI exceptions
- Confirm you have proper network access to the target server
When administering Windows servers, you'll often need to audit shared directories and their corresponding local paths. While the Computer Management console displays shares, extracting the underlying physical paths requires different approaches.
The simplest way to get both share names and local paths:
wmic share get name,path
Sample output:
Name Path
----- -------
ADMIN$ C:\Windows
C$ C:\
IPC$
Shared D:\CompanyData
For more detailed information and filtering capabilities:
Get-WmiObject -Class Win32_Share | Select-Object Name,Path,Description
Example with additional properties:
$shares = Get-WmiObject Win32_Share | Where-Object { $_.Path -ne $null }
$shares | Format-Table Name,Path,Status,Type -AutoSize
For programmatic access in .NET applications:
using System;
using System.Management;
class ShareEnumerator {
static void Main() {
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_Share");
foreach (ManagementObject share in searcher.Get()) {
Console.WriteLine($"Share: {share["Name"]}");
Console.WriteLine($"Path: {share["Path"] ?? "N/A"}");
Console.WriteLine("------------------");
}
}
}
Administrative shares (like C$ and ADMIN$) typically return empty paths. To resolve these:
# For C$ share:
$drive = ($_.Name -replace '\$','')
"\\$env:COMPUTERNAME\$($_.Name)" -replace '\$',':\'
Remember that accessing certain shares requires administrative privileges. Always run these commands/tools elevated when necessary.
For quick checks without scripting:
net share
While less detailed, this shows basic share information in a readable format.