Creating a share with Powershell

The following script allows you to create a share using PowerShell using the following command: CreateShare -Name “Share” -Path “C:\Share” -ReadAccess “Everyone” -FullAccess “Me”, “Other Admins”. See code below!

function CreateShare() {
[string] $Name,
[string] $Path,
[string[]] $ReadAccess,
[string[]] $FullAccess
#Check if directory is already there else create it
if ( -not (Test-Path $Path -PathType Container) ) { New-Item -Path $Path -ItemType directory }

#Create share in SMB:
Write-Host Creating share $Name for $Path
if(-not $ReadAccess) { New-SmbShare -Name $Name -Path $Path -FullAccess $FullAccess }
elseif(-not $FullAccess) { New-SmbShare -Name $Name -Path $Path -ReadAccess $ReadAccess }
else { New-SmbShare -Name $Name -Path $Path -ReadAccess $ReadAccess -FullAccess $FullAccess }

$Acl = Get-Acl -Path $Path

if($ReadAccess) {
$ReadAccess | ForEach {
Write-Host Granting $_ ReadAccess on $Path
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($_,"ReadAndExecute","ContainerInherit, ObjectInherit","None","Allow")

if($FullAccess) {
$FullAccess | ForEach {
Write-Host Granting $_ ReadAccess on $Path
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($_,"FullControl","ContainerInherit, ObjectInherit","None","Allow")

Write-Host Applying ACL to $Path
Set-Acl $Path $Acl

Leave a Reply

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