PowerShell: Create SMB Share and Grant NTFS Access

$fullAccessAccounts='everyone','authenticated users'

function createSmbShare($localPath,$fullAccessAccounts){
        # First, create SMB Share
        $smbShareName=split-path $localPath -leaf
        New-SmbShare -Name $smbShareName -Path $localPath -FullAccess $fullAccessAccounts

        # Second, set NTFS permissions
        $acl = Get-ACL $localPath
        foreach($account in $fullAccessAccounts){
            $allowFullAccesss=New-Object System.Security.AccessControl.FileSystemAccessRule($account,"Full","Allow")
        Set-Acl $localPath $acl
        write-host "\\$env:computername\$smbShareName has been created with full access granted to these users $fullAccessAccounts"
        write-warning $Error[0].Exception.Message
        return $false

createSmbShare $localPath $fullAccessAccounts

Leave a Reply

Your email address will not be published. Required fields are marked *