How To List All The Shares On A Server Using PowerShell

There’s a few ways to get all of the shared folders on a server, but not all of them work for all versions of Windows Server. You can use the Get-SmbShare cmdlet, or you can make CIM/WMI do the work for you. I’ll show you what I prefer, though.

To use Get-SmbShare on a remote computer, you’ll create a new CIM session.

PS> New-CimSession -ComputerName $computername -Credential $creds

Id           : 1
Name         : CimSession1
InstanceId   : 110928f2
ComputerName : computername
Protocol     : WSMAN

Then you can pass that CIM session to Get-SmbShare.

PS> Get-SmbShare -CimSession $(get-cimsession -id 1)

Name     ScopeName Path                              Description     PSComputerName
----     --------- ----                              -----------     --------------
ADMIN$   *         C:\windows                        Remote Admin    comp
C$       *         C:\                               Default share   comp
D$       *         D:\                               Default share   comp
IPC$     *                                           Remote IPC      comp
print$   *         C:\windows\system32\spool\drivers Printer Drivers comp
Profiles *         D:\Profiles                                       comp
Transfer *         C:\Shares\Transfer                                comp

But what if the server is (heaven forbid!) older than Windows Server 2012R2? Well, you’d get an error telling you “get-cimclass : The WS-Management service cannot process the request. The CIM namespace win32_share is invalid.”. That won’t do.

Well, luckily for those older servers, you can use Get-WmiObject to retrieve this information.

PS> Get-WmiObject -Class win32_share -ComputerName $oldComp -Credential $creds


Name         Path                                                  Description
----         ----                                                  -----------
ADMIN$       C:\windows                                            Remote Admin
C$           C:\                                                   Default share
D$           D:\                                                   Default share
IPC$                                                               Remote IPC

 

Written on September 6, 2017