Add user to a created share

Hi there.

Im a very very noob on Powershell and im trying to create a share and i´m stuck on add a user to the share.

Here is my script:

$user= Read-Host -Prompt User
$FolderPath = “c:$user”

$Shares=[WMICLASS]‘WIN32_Share’

$ShareName=“$user$”

New-Item -type directory -Path $FolderPath

$Shares.Create($FolderPath,$ShareName,0)

$Acl = Get-Acl $FolderPath
$Acl.SetAccessRuleProtection($True, $False)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(‘Administrators’,‘FullControl’,‘ContainerInherit, ObjectInherit’, ‘None’, ‘Allow’)
$Acl.AddAccessRule($rule)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(‘Teste’,‘Modify, Synchronize’, “ContainerInherit, ObjectInherit”, “None”, “Allow”)
$Acl.AddAccessRule($rule)

Get-Acl $FolderPath | Format-List
Set-Acl $FolderPath $Acl

Hi João,

I dont have a domain joined system nearby to test, but was able to achieve this on my workstation using the code below. Can you try this and let me know how you get on?

$user= Read-Host -Prompt User
$FolderPath = "c:\$user"

$Shares=[WMICLASS]'WIN32_Share'

$ShareName="$user$"

New-Item -type directory -Path $FolderPath

$Shares.Create($FolderPath,$ShareName,0)

$Acl = Get-Acl $FolderPath
$Acl.SetAccessRuleProtection($True, $False)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule('Administrators','FullControl','ContainerInherit, ObjectInherit', 'None', 'Allow')
$Acl.AddAccessRule($rule)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,'Modify, Synchronize', "ContainerInherit, ObjectInherit", "None", "Allow")
$Acl.AddAccessRule($rule)

Get-Acl $FolderPath | Format-List
Set-Acl $FolderPath $Acl

Hi Tim.

I tested on my workstation and it worked!

But apparently the script was fine? I cant see any changes beside the variable $user on line16

Thanks again