Managing File Shares on Windows with PowerShell (Part 1)

by Mar 7, 2022

Windows comes with a module called “SMBShare” which contains 42 cmdlets to manage network shares. This module works with Windows PowerShell and PowerShell 7:

 
PS> Get-Command -Module SMBShare 

CommandType Name                               Version Source  
----------- ----                               ------- ------  
Function    Block-SmbShareAccess               2.0.0.0 SMBShare
Function    Close-SmbOpenFile                  2.0.0.0 SMBShare
Function    Close-SmbSession                   2.0.0.0 SMBShare
Function    Disable-SmbDelegation              2.0.0.0 SMBShare
Function    Enable-SmbDelegation               2.0.0.0 SMBShare
Function    Get-SmbBandwidthLimit              2.0.0.0 SMBShare
Function    Get-SmbClientConfiguration         2.0.0.0 SMBShare
Function    Get-SmbClientNetworkInterface      2.0.0.0 SMBShare
Function    Get-SmbConnection                  2.0.0.0 SMBShare
Function    Get-SmbDelegation                  2.0.0.0 SMBShare
Function    Get-SmbGlobalMapping               2.0.0.0 SMBShare
Function    Get-SmbMapping                     2.0.0.0 SMBShare
Function    Get-SmbMultichannelConnection      2.0.0.0 SMBShare
Function    Get-SmbMultichannelConstraint      2.0.0.0 SMBShare
Function    Get-SmbOpenFile                    2.0.0.0 SMBShare
Function    Get-SmbServerCertificateMapping    2.0.0.0 SMBShare
Function    Get-SmbServerConfiguration         2.0.0.0 SMBShare
Function    Get-SmbServerNetworkInterface      2.0.0.0 SMBShare
Function    Get-SmbSession                     2.0.0.0 SMBShare
Function    Get-SmbShare                       2.0.0.0 SMBShare
Function    Get-SmbShareAccess                 2.0.0.0 SMBShare
Function    Grant-SmbShareAccess               2.0.0.0 SMBShare
Function    New-SmbGlobalMapping               2.0.0.0 SMBShare
Function    New-SmbMapping                     2.0.0.0 SMBShare
Function    New-SmbMultichannelConstraint      2.0.0.0 SMBShare
Function    New-SmbServerCertificateMapping    2.0.0.0 SMBShare
Function    New-SmbShare                       2.0.0.0 SMBShare
Function    Remove-SmbBandwidthLimit           2.0.0.0 SMBShare
Function    Remove-SmbComponent                2.0.0.0 SMBShare
Function    Remove-SmbGlobalMapping            2.0.0.0 SMBShare
Function    Remove-SmbMapping                  2.0.0.0 SMBShare
Function    Remove-SmbMultichannelConstraint   2.0.0.0 SMBShare
Function    Remove-SmbServerCertificateMapping 2.0.0.0 SMBShare
Function    Remove-SmbShare                    2.0.0.0 SMBShare
Function    Revoke-SmbShareAccess              2.0.0.0 SMBShare
Function    Set-SmbBandwidthLimit              2.0.0.0 SMBShare
Function    Set-SmbClientConfiguration         2.0.0.0 SMBShare
Function    Set-SmbPathAcl                     2.0.0.0 SMBShare
Function    Set-SmbServerConfiguration         2.0.0.0 SMBShare
Function    Set-SmbShare                       2.0.0.0 SMBShare
Function    Unblock-SmbShareAccess             2.0.0.0 SMBShare
Function    Update-SmbMultichannelConnection   2.0.0.0 SMBShare   
 

To start finding your way into these cmdlets, start with those that use the verb “Get”: they read information and can’t accidentally change system settings.

For example, Get-SmbShare lists all the network shares available on your machine:

 
PS> Get-SmbShare 

Name                        ScopeName Path                                 Description
----                        --------- ----                                 -----------
ADMIN$                      *         C:\WINDOWS                           Remoteadmi...
C$                          *         C:\                                  Standardfr...
HP Universal Printing PCL 6 *         S/W Laser HP,LocalsplOnly            S/W Laser HP 
IPC$                        *                                              Remote-IPC
OKI PCL6 Class Driver 2     *         OKI PCL6 Class Driver 2,LocalsplOnly OKI PCL6 C...
print$                      *         C:\Windows\system32\spool\drivers    Printerdr...
 

To discover how to add, configure or remove SmbShares, try looking at cmdlets with the noun “smbshare”:

 
PS> Get-Command -Module SMBShare -Noun SmbShare 

CommandType Name            Version Source  
----------- ----            ------- ------  
Function    Get-SmbShare    2.0.0.0 SMBShare
Function    New-SmbShare    2.0.0.0 SMBShare
Function    Remove-SmbShare 2.0.0.0 SMBShare
Function    Set-SmbShare    2.0.0.0 SMBShare  
 

New-SmbShare lets you add a new basic network share. Before you go ahead and run commands that change your system, it’s a good idea to read the cmdlet documentation and look at the examples included:

 
PS> Get-Help -Name New-SmbShare -Online   
 

This opens the documentation page in your default browser. The documentation explains the available parameters plus provides examples like this:

 
PS> New-SmbShare -Name VMSFiles -Path C:\ClusterStorage\Volume1\VMFiles -FullAccess Contoso\Administrator, Contoso\Contoso-HV1$   
 

It illustrates how simple it is to create a new file share and secure it with access permissions. You’d have to adjust the parameters of the example before you run the command, and at minimum update the local folder path you want to share, plus the account names that are supposed to have full access.


Twitter This Tip! ReTweet this Tip!