How can I create a folder, set permissions and then share the folder and set share permissions with PowerShell?

Problem:

How can I create a folder, set permissions and then share the folder and set share permissions with PowerShell?

Run the below script:

Add-Type -AssemblyName Microsoft.VisualBasic

$NewFolderPath = [Microsoft.VisualBasic.Interaction]::InputBox('Enter folder path', 'Folder path')

$NewFolderName = [Microsoft.VisualBasic.Interaction]::InputBox('Enter folder name', 'Folder name')

New-Item -Path $NewFolderPath -Name $NewFolderName -ItemType Directory


$AclPath = $NewFolderPath + $NewFolderName
$Acl = Get-Acl -Path $AclPath

$acl = Get-Acl $AclPath
$acl.SetAccessRuleProtection($true,$false)
$acl | Set-Acl $AclPath

$acl = Get-Acl $AclPath
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("admin","FullControl", "ContainerInherit,ObjectInherit", 'None', "Allow")
$acl.SetAccessRule($AccessRule)
$acl | Set-Acl $AclPath

$acl = Get-Acl $AclPath
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("nasadmin","FullControl", "ContainerInherit,ObjectInherit", 'None', "Allow")
$acl.SetAccessRule($AccessRule)
$acl | Set-Acl $AclPath

$acl = Get-Acl $AclPath
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("nasuser","ReadAndExecute", "ContainerInherit,ObjectInherit", 'None', "Allow")
$acl.SetAccessRule($AccessRule)
$acl | Set-Acl $AclPath

$acl = Get-Acl $AclPath
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("Administrators","FullControl", "ContainerInherit,ObjectInherit", 'None', "Allow")
$acl.SetAccessRule($AccessRule)
$acl | Set-Acl $AclPath

New-SmbShare -Name $NewFolderName -Path $AclPath -FullAccess "Everyone" 

Leave a Reply

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