DEV Community 👩‍💻👨‍💻

Cover image for Managing File Shares with PowerShell
Adam the Automator
Adam the Automator

Posted on

Managing File Shares with PowerShell

This article was written by David Lamb. If you'd like to read more from this author, check out his ATA author page. Be sure to also check out more how-to posts on cloud computing, system administration, IT, and DevOps on!

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 get-smbshare makes this task a snap, whether you need to enumerate shares on just one server, or many.

If you're a beginner/intermediate PowerShell scripter, be sure to check out my FREE mini-course on Building a PowerShell Tool! 9,000+ words of deep explanation and insights on how to build a PowerShell tool.

This blog post has a companion video created by TechSnips contributor, David Lamb. Feel free to have a watch or, if you prefer text, read on!

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.

PS> Enter-PSSession -ComputerName FILE01

Once connected, it takes a single cmdlet to get file share information get-smbshare.

Alt Text

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:

Alt Text

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:

Alt Text

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-smbshare access cmdlet.

Alt Text

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:

Alt Text

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:

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

PS> Invoke-Command -ComputerName "FILE01","FILE02" -ScriptBlock {Get-SmbShare} | Format-Table -Property Name,Path,Description,PSComputerName

Alt Text

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:

$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

Alt Text

There we have it! A nice tidy list of all of the file shares on all of our file servers.


In this blog, you have learned how to enumerate file shares using the get-smbshare cmdlet.

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.