It can be challenging to keep track of just what file shares have been set up in your environment. This becomes even more difficult if you have to track this information across multiple servers. Adding to the tedium is remotely connecting to each server to find the list the shares. Thankfully, using PowerShell makes this task a snap, whether you need to enumerate shares on just one server, or many.
Enumerate Shares on a Single File Server
Let’s start by connecting to a remote file server to gather this information from a single server. We will accomplish this by entering into a remote PowerShell session with our file server “FILE01”.
1 |
Enter-PSSession -ComputerName FILE01 |
Once connected, it takes a single cmdlet to get file share information:
1 |
Get-SmbShare |
As you can see, this gives us a list of all of the share on this server. This also includes the administrative shares, whose share names are appended by $
.
This does accomplish the task of getting a list of shares, but it is a little cluttered. We can clean up this list by using the -Special
parameter and setting it to $false
to specify that we do not wish to see the administrative shares:
1 |
Get-SmbShare -Special $false |
There, that gives us a much clearer view of the share information we are looking for.
Now that we have our share on this server identified, it might be useful to list all of the properties for this share, especially if we are looking for specific details about our share:
1 |
Get-SmbShare -Name Presentations | Select-Object -Property * |
This allows us to view quite a bit of information about our share, including things like the type of share, folder enumeration mode, caching mode, and of course, our share name and path, to name a few.
It is also possible to view the share permissions for this share by switching to the Get-SmbShareAccess
cmdlet:
1 |
Get-SmbShareAccess -Name Presentations |
This gives us a list of the users and groups, and their current level of access to the share.
We might also have a time where we need to enumerate the share permissions to find out who has full access to a share:
1 |
Get-SmbShareAccess -Name Presentations | Where-Object AccessRight -EQ "Full" |
With this information, it is easy to tell who has full access to the share and then take steps to remove that access if it isn’t appropriate for an individual or group.
Now that we are done enumerating shares on a single server, we need to make sure we close our remote PowerShell session:
1 |
Exit-PSSession |
Enumerate Shares on Multiple File Servers
It is also possible to retrieve this same information from multiple file servers, which is an area where PowerShell really shines. Using Invoke-Command
to run Get-SmbShare
, we can list the shares on both the FILE01 and FILE02 servers. If we also pipe the output through Format-Table
, we can also get a nice organized list:
1 |
Invoke-Command -ComputerName "FILE01","FILE02" -ScriptBlock {Get-SmbShare} | Format-Table -Property Name,Path,Description,PSComputerName |
While entering the file server names manually is fine if there are only two or three servers, it becomes tedious if there are many dozens of servers to check. To get around this, we can assign the output of Get-ADComputer
to the variable $FileServAD
and get a list of all servers in the “File Servers” Organizational Unit (OU). From there, it’s easy to get the information:
1 2 |
$FileServAD = Get-ADComputer -SearchBase "OU=File Servers,OU=Servers,DC=corp,dc=ad" -Filter * | Select-Object -ExpandProperty Name Invoke-Command -ComputerName $FileServAD -ScriptBlock {Get-SmbShare -Special $false} | Format-Table -Property Name,Path,Description,PSComputerName |
There we have it! A nice tidy list of all of the file shares on all of our file servers.
Additional Resources
Companion Video: “How To Enumerate File Shares On A Remote Windows Computer With PowerShell“
David Lamb is a Systems Administrator managing Windows servers and clients since 1995, spending a large portion of his career in the aviation industry. His first certification was the MCSE on Windows NT 4.0, earned in 2001. David lives in Alberta, Canada, and is currently spending his free time learning PowerShell, blogging, and pursuing the MCSE certification on Windows Server.