Using Powershell to Create Windows SMB Shares

Published on August 9, 2016 under Microsoft · Tagged Windows and Powershell · Read in about 1 min (180 words)

I have been knee deep in migrations, so after clicking through a few times to create clustered file shares, I finally decided to script them. Here is that script I thought I would share. Thanksfully there was the NTFSSecurity module published by microsoft that makes permissions a breeze. You can get more details by viewing the module page.


Preparing to Execute the Script

Fill in the appropriate variables, and execute the script.

Import-Module NTFSSecurity

$sharename = "share"
$cachingmode = "none"
$continuouslyavailable = $true
$encryptdata = $false
$folderenumerationmode = "AccessBased"
$path = "D:\Shares\"
$scopename = "ClusterORResourceName"
$sharepath = $path+$sharename
$fullaccess = "Everyone"

#check if path exists
$PathCheck = Test-Path $sharepath
if ($PathCheck -eq $true) {
    Write-Host "$sharepath exists."
else {
    Write-Host "$sharepath does not exist"
    New-Item $sharepath -type directory

#create share
New-SmbShare -Name $sharename -CachingMode $cachingmode -ContinuouslyAvailable $continuouslyavailable -EncryptData $encryptdata -FolderEnumerationMode $folderenumerationmode -Path $sharepath -ScopeName $scopename -FullAccess $fullaccess

#disable inheritance and add full access
Disable-NTFSAccessInheritance $sharepath -RemoveInheritedAccessRules
Add-NTFSAccess $sharepath -Account BUILTIN\Administrators -AccessRights FullControl
Get-NTFSAccess $sharepath

Execute the Script

  • Run .\Create-SMBShare.ps1
Share this post
comments powered by Disqus

See Also