Managing File Shares with PowerShell
Tags: powershell
Published: 2021-01-14 20:26:03

> _This article was written by [David Lamb](https://adamtheautomator.com/author/david/). If you'd like to read more from this author, check out his [ATA author page](http://adamtheautomator.com/author/david). Be sure to also check out [more how-to posts on cloud computing, system administration, IT, and DevOps on adamtheautomator.com](https://adamtheautomator.com)!_

<!-- wp:paragraph -->
<p>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 <a href="https://adamtheautomator.com/how-to-set-up-and-manage-scheduled-tasks-with-powershell/">PowerShell get-smbshare makes this task</a> a snap, whether you need to enumerate shares on just one server, or many.</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><em>If you're a beginner/intermediate PowerShell scripter, be sure to check out my <a href="https://adamtheautomator.com/powershell-tutorial-mini-course/">FREE mini-course on Building a PowerShell Tool</a>! 9,000+ words of deep explanation and insights on how to build a PowerShell tool.</em></p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>This blog post has a companion video created by <a href="https://techsnips.io/contributors/david-lamb/">TechSnips contributor, David Lamb</a>. Feel free to have a watch or, if you prefer text, read on!</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="enumerate-shares-on-a-single-file-server">Enumerate Shares on a Single File Server</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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 <em>FILE01</em>.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code lang="powershell" class="language-powershell">PS&gt; Enter-PSSession -ComputerName FILE01</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Once connected, it takes a single cmdlet to get file share information get-smbshare.</p>
<!-- /wp:paragraph -->

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0qn5khqafdvnh5dx7bk7.png)

<!-- wp:paragraph -->
<p>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 <code>$</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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 <code>Special</code> parameter and setting it to <code>$false</code> to specify that we do not wish to see the administrative shares:</p>
<!-- /wp:paragraph -->

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nufme1cne1c9swfc2s6l.png)

<!-- wp:paragraph -->
<p>There, that gives us a much clearer view of the share information we are looking for.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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:</p>
<!-- /wp:paragraph -->

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ww20kd56l106nxpk0ofx.png)

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It is also possible to view the share permissions for this share by switching to the get-smbshare access cmdlet.</p>
<!-- /wp:paragraph -->

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ug44yjy45yzm6omeu46y.png)

<!-- wp:paragraph -->
<p>This gives us a list of the users and groups, and their current level of access to the share.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We might also have a time where we need to enumerate the share permissions to find out who has full access to a share:</p>
<!-- /wp:paragraph -->

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dstehk64yeapxks9hhai.png)

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Now that we are done enumerating shares on a single server, we need to make sure we close our remote PowerShell session:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code lang="powershell" class="language-powershell">PS&gt; Exit-PSSession</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2 id="enumerate-shares-on-multiple-file-servers">Enumerate Shares on Multiple File Servers</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It is also possible to retrieve this same information from multiple file servers, which is an area where PowerShell really shines.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Using <code><a class="wpil_keyword_link" href="https://adamtheautomator.com/invoke-command-remote/" title="Invoke-Command" data-wpil-keyword-link="linked">Invoke-Command</a></code> to run get-smbshare, we can list the shares on both the <em>FILE01</em> and <em>FILE02</em> servers. If we also pipe the output through <code>Format-Table</code>, we can also get a nice organized list:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code lang="powershell" class="language-powershell">PS&gt; Invoke-Command -ComputerName "FILE01","FILE02" -ScriptBlock {Get-SmbShare} | Format-Table -Property Name,Path,Description,PSComputerName</code></pre>
<!-- /wp:code -->

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0zj36c5oqq1vc51odtg2.png)

<!-- wp:paragraph -->
<p>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 <code><a class="wpil_keyword_link" href="https://adamtheautomator.com/get-adcomputer-powershell/" title="Get-ADComputer" data-wpil-keyword-link="linked">Get-ADComputer</a></code> to the variable <code>$FileServAD</code> and get a list of all servers in the <em>File Servers</em> organizational unit (OU). From there, it's easy to get the information:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code lang="powershell" class="language-powershell">$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</code></pre>
<!-- /wp:code -->

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9wpfnkthhuptkt1bjl1e.png)

<!-- wp:paragraph -->
<p>There we have it! A nice tidy list of all of the file shares on all of our file servers.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="summary">Summary</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In this blog, you have learned how to enumerate file shares using the get-smbshare cmdlet.</p>
<!-- /wp:paragraph -->