Hi All,

Can someone help me to build powershell script.

I need to put groups in folder permission

example : folder name - ALCDSB
I need to add a group called ALCDSB_Central with Modify permission

There are 500 folders with different name
We want to add only those group whose name match with folder like mentioned below:

$foldername for that folder I want to add only group whose name is like $foldername_Central*

-----------------------------------------------------code--------------------------------------

$folderslist= Import-Csv C:\archiveSeverTest\folderlist.csv
$groupname= import-csv C:\archiveSeverTest\groupname.csv

foreach ($f in $folderslist){

$foldername=$f.name

foreach ($g in $groupname){

$gname=$g.name

if ( $gname=“$($foldername)central”) {

$acl = Get-Acl “C:\archiveSeverTest\21$($foldername)”
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule “$($gname)”,“Modify”,“Allow”
$acl.SetAccessRule($AccessRule)

$acl | Set-Acl “C:\archiveSeverTest\21$($foldername)”
}

else {
echo “group don’t match”
}
}

}

error code :

a5c73cf0-f2b3-4efb-b75f-4544cbaa4a08-dd.PNG

2 Spice ups

Happy to HELP but not a script writing service.

What have you tried? Where are you stuck?

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

1 Spice up

Yes, you can do this, but why? If you seriously have 500 separate groups, you will have a huge job keeping those groups up to date. What is the issue???

I have uploaded my code with error message.

Any help would be really appreciated

@thomaslee

we are putting this for some archiving servies. All the gruops members are updated via scheduled task. Issue is I need to assign all those groups with their associated folders now.

Can you please check the code and highlight where I am making mistakes

There is no code, you just posted a picture of an error message ?

Did you see, if you post code, please use the insert code button?

codebutton_small.png

1 Spice up

@alexw ​ insert code button is not working.

There are several things you need to change.

What does your CSV file look like? Does it have headers? Can you post a sample?

 if ( $gname="$($foldername)*central*") {

in powershell to compare you use ‘-eq’ -or ‘-like’ for strings
If you use ‘=’ that’s a value assignment

so it should be more like so

if ( $gname -like "$($foldername)*central*") {

as for the error you are getting with the identity reference, you can not just use the group name, you have to tell it the domain name.

$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("domain\$gname","Modify","Allow")

Have you tried a different browser? Works for me in Google Chrome, Edge and Firefox?

Hi @alexw ​,
I have made the changes as per suggestions. But now its going into the else statement, printing “group don’t match”. none of the folders permissions are updated.

My csv files are looking like this
folderlist csv 64838ead-9290-4934-8371-8f8fc662631a-folderlistcsv.PNG
groupname csv

$folderslist= Import-Csv C:\archiveSeverTest\folderlist.csv
$groupname= import-csv C:\archiveSeverTest\groupname.csv

foreach ($f in $folderslist){

    $foldername=$f.name 

    foreach ($g in $groupname){
        
        $gname=$g.name

        if ( $gname -like "$($foldername)*central*") {
        
            $acl = Get-Acl "C:\archiveSeverTest\21\$($foldername)"
            $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule ("mwood\$gname","Modify","Allow")
            $acl.SetAccessRule($AccessRule)

            $acl | Set-Acl "C:\archiveSeverTest\21\$($foldername)"
        }

        else {
            echo "group don't match"
        }
    }

} 

That type line seems to be your header , not ‘name’
So either remove the type row, or adjust the code

Deleted the first row from both csv file.
Still getting the same output. I don’t have any further clue.