Powershell script to create Parent folder and sub folder from csv and permission

Hello,

Have to create multiple Parent folders with permissions and In side of Each Parent folders need to create 04 Sub folders. In this subfolders 02 Folders Inheritance should be disabled

I found the below script it will create the Parent folders with Permission mentioned in the CSV. Please help me to create subfolders inside each Parent folders with Modify Permission  level




#$path = "\\localhost\Share\"
$csvFile = "C:\Users\Administrator\Desktop\create.csv"

$create = Import-CSV $csvFile

function DoPermissions
{
    param( $permissionGroup, $folder, $level)
    $toAdd = $permissionGroup -split ";"
    Write-Host $folder
    foreach ($item in $toAdd)
    {
        $acl = (Get-Item $folder).GetAccessControl('Access')
        $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($item, $level, 'ContainerInherit,ObjectInherit','None','Allow')
        $acl.SetAccessRule($ar)
        Set-ACL -path $folder -AclObject $acl
    }
}

foreach ($folder in $create)
{
    $fullPath = $folder.folder #$path + $folder.folder
    if (!(Test-Path $fullPath)) {New-Item -ItemType Directory -Path $fullPath}

    if ($folder.full_control) {DoPermissions $folder.full_control $fullPath "FullControl"}
    if ($folder.modify) {DoPermissions $folder.modify $fullPath "Modify"}
    if ($folder.read_execute) {DoPermissions $folder.read_execute $fullPath "ExecuteFile"}
    if ($folder.list_folder_content) {DoPermissions $folder.list_folder_content $fullPath "ListDirectory"}
    if ($folder.read) {DoPermissions $folder.read $fullPath "ReadData"}
    if ($folder.write) {DoPermissions $folder.write $fullPath "Write"}

}

Open in new window

LVL 3
SAM ITAD windows Admin Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SAM ITAD windows Admin  Author Commented:
I am using CSV in below format

folder	full_control	modify	read_execute	list_folder_content	read	write
folderxyz		DL-WP-uu-test-Projects-XXXXXX-RW-TEST				

Open in new window

0
SAM ITAD windows Admin  Author Commented:
Examples attached to create sub directory . Can we merge this script to above mentioned one ?

$importFile = "C:\Users\a.narasimha.s\Desktop\directory.csv"
$csvContent = Import-Csv $importFile
$baseDirectory = "C:\Users\a.narasimha.s\Desktop\98"
foreach($line in $csvContent) {
    $currentFolder = $baseDirectory + $line.Directory
    if (!(Test-Path $currentFolder)) {
        New-Item $currentFolder -type directory
        for ($i = 1;$i -le 5;$i++) {
            New-Item "$currentFolder\sub$i" -type directory
        }
    }
}
Write-Host "Done"

Open in new window

0
SAM ITAD windows Admin  Author Commented:
Examples attached to create sub directory . Can we merge this script to above mentioned one ?


$root = "C:\Users\a.narasimha.s\Desktop\foldertest1"

ForEach ($dir in (Get-ChildItem -Path "$root\*\" | ?{$_.PSIsContainer} | Select -Expand Name)){



  New-Item -Path "$dir\folderb" -ItemType Directory | Out-Null
  New-Item -Path "$dir\foldera" -ItemType Directory | Out-Null
  New-Item -Path "$dir\1" -ItemType Directory | Out-Null
  New-Item -Path "$dir\2" -ItemType Directory | Out-Null

 }

Open in new window

0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Joe KlimisCommented:
I have combined the two   please test and let me know if you are still having problems


$csvfile = "c:\temp\1.csv"


#  used to create the tab dillimited file
<#
$csvfilecontent = @"
folder`tOwners`tfull_control`tmodify`tread_execute`tlist_folder_content`tread`twrite
folderxyz`tDomain Admins`ty					
folderxy1`tAdministrators;Domain Admins`t`t`t`t`ty		
folder3`tUsers;Domain Users`t`ty`ty	
"@

$csvfilecontent | out-file $csvfile
#>

$root ="C:\temp\test1\"    # make sure has a trailing "\"

$csvdata = Import-CSV  "c:\temp\1.csv" -Delimiter "`t"

function DoPermissions
{
    param( $permissionGroup, $folder, $level)
    $toAdd = $permissionGroup -split ";"
    Write-Host $folder
    foreach ($item in $toAdd)
    {
#       
        $acl  = get-acl $folder
        $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($item, $level, 'ContainerInherit,ObjectInherit','None','Allow')
        $acl.SetAccessRule($ar)
        Set-ACL -path $folder -AclObject $acl
    }
}

foreach ($folder in $csvdata)
{
$folder
# create top level folder
    $fullPath = "$root$($folder.folder)"
    if (!(test-path $fullpath -PathType Container)) {New-Item -ItemType Directory -Path $fullPath}
 # set permissions on top level folder
    if ($folder.full_control) {DoPermissions $folder.owners $fullPath "FullControl"}
    if ($folder.modify) {DoPermissions $folder.owners $fullPath "Modify"}
    if ($folder.read_execute) {DoPermissions $folder.owners $fullPath "ExecuteFile"}
    if ($folder.list_folder_content) {DoPermissions $folder.owners $fullPath "ListDirectory"}
    if ($folder.read) {DoPermissions $folder.owners $fullPath "ReadData"}
    if ($folder.write) {DoPermissions $folder.owners $fullPath "Write"}
# define the subfolder names
	$subfolders = "Subfolder1","SubFolder2","SubFolder3","Subfolder4"
	
	for ($i = 1;$i -lt 4;$i++) 
		{ # create the sub folder
        $subpath = "$fullPath\$($subfolders[$i])"
                if (!(test-path $subpath -PathType Container)) {New-Item $subpath -type directory}
		# set the sub folder permissions
			switch ($i) {
			1 {DoPermissions $folder.owners $fullPath "ReadData"}
			2 {DoPermissions $folder.owners $fullPath "ReadData";DoPermissions $folder.owners $fullPath "Write"}
			3 {DoPermissions $folder.owners $fullPath "ReadData"}
			4 {DoPermissions $folder.owners $fullPath "ReadData";DoPermissions $folder.owners $fullPath "ListDirectory"}
			
			}
        }
	



}


    

Open in new window

0
SAM ITAD windows Admin  Author Commented:
Getting error , Error screen shot attached

output was Creating only Subfolders on the Root . Parent folders are not getting created.

                                                            OR else

Below script worked fine. It creates the folders in the Parent folders. After sub folders creates Permissions are in inherited from Parent  folder.

1. Need to disable In inheritance on Sub folders
2. In inherited Group1 and Group2 Is Modify level access . need to set the Read access level for Group1 and Group . You can utilize the below script

$root = "C:\Users\a.narasimha.s\Desktop\foldertest1"

ForEach ($dir in (Get-ChildItem -Path "$root\*\" | ?{$_.PSIsContainer} | Select -Expand Name)){



  New-Item -Path "$dir\folder1" -ItemType Directory | Out-Null
  New-Item -Path "$dir\folder2" -ItemType Directory | Out-Null
  New-Item -Path "$dir\1" -ItemType Directory | Out-Null
  New-Item -Path "$dir\2" -ItemType Directory | Out-Null

 }

Open in new window

error2.jpg
0
Joe KlimisCommented:
I think the problem causing the errors is the information in your csv file, please run the following , and check to see if the format matches with yours, as the AD accounts/ groups are not listed in you example CSV   . I think you need to add the owners column in your CSV  with corresponding account and it will work fine.   to test this run the following code

$csvfile = "c:\temp\1.csv"
$csvfilecontent = @"
folder`tOwners`tfull_control`tmodify`tread_execute`tlist_folder_content`tread`twrite
folderxyz`tDomain Admins`ty					
folderxy1`tAdministrators;Domain Admins`t`t`t`t`ty		
folder3`tUsers;Domain Users`t`ty`ty	
"@
$csvfilecontent | out-file $csvfile

Open in new window




your other point  to change the names of the subfolder to match your example change the following line from

$subfolders = "Subfolder1","SubFolder2","SubFolder3","Subfolder4"

Open in new window


to

$subfolders = "folder1","Folder2","1","2"

Open in new window


Please let me know how you get on.
0
SAM ITAD windows Admin  Author Commented:
Hello Joe,

I have tested the below script. The output is almost we reached the expectation. It will create the Parent folders which are mentioned in CSV and creates the sub folders on each Parent folder created. Now in the below script You have mentioned 04 subfolders to be created. like

subfolder1 to subfolder 4 . After executing the script subfolder 1 is not getting created. And I want to create 07 subfolders insteaed of 04 subfolders. I tried changing script line for ($i = 1;$i -lt 4;$i++) from 4 to 7 and tried but got an error . please assist

And sub folder creates only on Last folder which is metioned in the Parent folder CSV input. please find the below CSV format

folder      full_control      modify      read_execute      list_folder_content      read      write
folderxyz            DL-WGK-US-Houston-Projects-XXXXXX-RW-TEST                        
new                                    
old folder                                    

 
Set-Location "C:\Users\IBM_ADMIN\Desktop\foldertest"
$csvFile = "C:\Users\IBM_ADMIN\Desktop\import.csv"

$create = Import-CSV $csvFile

function DoPermissions
{
    param( $permissionGroup, $folder, $level)
    $toAdd = $permissionGroup -split ";"
    Write-Host $folder
    foreach ($item in $toAdd)
    {
        $acl = (Get-Item $folder).GetAccessControl('Access')
        $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($item, $level, 'ContainerInherit,ObjectInherit','None','Allow')
        $acl.SetAccessRule($ar)
        Set-ACL -path $folder -AclObject $acl
    }
}

foreach ($folder in $create)
{
    $fullPath = $folder.folder #$path + $folder.folder
    if (!(Test-Path $fullPath)) {New-Item -ItemType Directory -Path $fullPath}

    if ($folder.full_control) {DoPermissions $folder.full_control $fullPath "FullControl"}
    if ($folder.modify) {DoPermissions $folder.modify $fullPath "Modify"}
    if ($folder.read_execute) {DoPermissions $folder.read_execute $fullPath "ExecuteFile"}
    if ($folder.list_folder_content) {DoPermissions $folder.list_folder_content $fullPath "ListDirectory"}
    if ($folder.read) {DoPermissions $folder.read $fullPath "ReadData"}
    if ($folder.write) {DoPermissions $folder.write $fullPath "Write"}



}
# define the subfolder names
	$subfolders = "Subfolder1","confidential","SubFolder3","Subfolder4","document"
	
	for ($i = 1;$i -lt 4;$i++) 
		{ # create the sub folder
        $subpath = "$fullPath\$($subfolders[$i])"
                if (!(test-path $subpath -PathType Container)) {New-Item $subpath -type directory}
		# set the sub folder permissions
			switch ($i) {
			1 {DoPermissions $folder.owners $fullPath "ReadData"}
			2 {DoPermissions $folder.owners $fullPath "ReadData";DoPermissions $folder.owners $fullPath "Write"}
			3 {DoPermissions $folder.owners $fullPath "ReadData"}
			4 {DoPermissions $folder.owners $fullPath "ReadData";DoPermissions $folder.owners $fullPath "ListDirectory"}
			
			}
        }
	

Open in new window

0
Joe KlimisCommented:
Hi

Please Try the following

Set-Location "C:\Users\IBM_ADMIN\Desktop\foldertest"
$csvFile = "C:\Users\IBM_ADMIN\Desktop\import.csv"


$create = Import-CSV $csvFile 

function DoPermissions
{
    param( $permissionGroup, $folder, $level)
    $toAdd = $permissionGroup -split ";"
    Write-Host $folder
    foreach ($item in $toAdd)
    {
        $acl = (Get-Item $folder).GetAccessControl('Access')
        $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($item, $level, 'ContainerInherit,ObjectInherit','None','Allow')
        $acl.SetAccessRule($ar)
        Set-ACL -path $folder -AclObject $acl
    }
}

foreach ($folder in $create)
{

    $fullPath = $folder.folder #$path + $folder.folder
    if (!(Test-Path $fullPath)) {New-Item -ItemType Directory -Path $fullPath}

    if ($folder.full_control) {DoPermissions $folder.full_control $fullPath "FullControl"}
    if ($folder.modify) {DoPermissions $folder.modify $fullPath "Modify"}
    if ($folder.read_execute) {DoPermissions $folder.read_execute $fullPath "ExecuteFile"}
    if ($folder.list_folder_content) {DoPermissions $folder.list_folder_content $fullPath "ListDirectory"}
    if ($folder.read) {DoPermissions $folder.read $fullPath "ReadData"}
    if ($folder.write) {DoPermissions $folder.write $fullPath "Write"}




# define the subfolder names
	$subfolders = "Subfolder1","confidential","SubFolder3","Subfolder4","document","six","seven"
	
	for ($i = 0;$i -le 6;$i++) 
		{ # create the sub folder
        $subpath = "$fullPath\$($subfolders[$i])"
                if (!(test-path $subpath -PathType Container)) {New-Item $subpath -type directory}
		# set the sub folder permissions
			switch ($i) {
			0 {DoPermissions $folder.owners $fullPath "ReadData"}
			1 {DoPermissions $folder.owners $fullPath "ReadData";DoPermissions $folder.owners $fullPath "Write"}
			2 {DoPermissions $folder.owners $fullPath "ReadData"}
			3 {DoPermissions $folder.owners $fullPath "ReadData";DoPermissions $folder.owners $fullPath "ListDirectory"}
			4 {DoPermissions $folder.owners $fullPath "ReadData";DoPermissions $folder.owners $fullPath "Write"}
			5 {DoPermissions $folder.owners $fullPath "ReadData"}
			6 {DoPermissions $folder.owners $fullPath "ReadData";DoPermissions $folder.owners $fullPath "ListDirectory"}
			
			}
        }

        }
	

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SAM ITAD windows Admin  Author Commented:
I have sorted how this some how .

Now am facing issue assigning multiple AD security group modify level for Parent folders. below is the open ticket

https://www.experts-exchange.com/questions/29018227/Import-CSV-with-All-modify-groups.html?anchor=a42106380¬ificationFollowed=187997725#a42106380
0
SAM ITAD windows Admin  Author Commented:
Issue sorted out by using semi colon ; on each coloum. Thanks for your time
0
Jason CrawfordTransport NinjaCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Joe Klimis (https:#a42106561)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

exchangepro
Experts-Exchange Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.