Scripting Windows Platform

Powershell Script, how to create folder and sub folder with permission from Network

Let me show you how to use Powershell Script to create folder and subfolder with different permission access from a Array variable with the graphic interface. We will use Get-ChildItem and foreach to list in a drop down menu the network folder list. We will also use system.security.accesscontrol.filesystemaccessrule and SetAccessRule for the folders permissions.

*****  Script  ************

Function create {

$NewFolder = $newfolderName.text
$MailnFolder = $Folderlist.SelectedItem.ToString()

# ****** Main folder List creation ****

$ListePerm = “\\AUDAIN-SERVER\AUD Folders\$MailnFolder\$NewFolder\Software”,”\\AUDAIN-SERVER\AUD Folders\$MailnFolder\$NewFolder\Tech”,”\\AUDAIN-SERVER\AUD Folders\$MailnFolder\$NewFolder\admin”,”\\AUDAIN-SERVER\AUD Folders\$MailnFolder\$NewFolder\RH”,

for ($i=0; $i -lt $ListePerm.length; $i++) {

new-item -type directory -path $ListePerm[$i]

# ****** Do not apply inheritance in folder RH ****
If ($i -eq 6) {
$Acl = Get-Acl $ListePerm[$i]
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule(“Local Employee”,”FullControl”,”Allow”)
$Acl.SetAccessRule($Ar)
Set-Acl -path $ListePerm[$i] -AclObject $Acl
}
else
{
$Acl = Get-Acl $ListePerm[$i]
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule(“Local Employee”,”FullControl”,”ObjectInherit, ContainerInherit”,”None”,”Allow”)
$Acl.SetAccessRule($Ar)
Set-Acl -path $ListePerm[$i] -AclObject $Acl

}
}

# ****** Creation of Sub folder “Software” ****

$LsoftPath = $ListePerm[1]

$Listesoft = “$LsoftPath\Windows”,”$LsoftPath\SQL”,”$LsoftPath\Apple”,”$LsoftPath\Linux”

for ($i=0; $i -lt $Listesoft.length; $i++) {

new-item -type directory -path $Listesoft[$i]

$Acl = Get-Acl $Listesoft[$i]
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule(“Local Employee”,”FullControl”,”ObjectInherit, ContainerInherit”,”None”,”Allow”)
$Acl.SetAccessRule($Ar)
Set-Acl -path $Listesoft[$i] -AclObject $Acl

}

# ****** Creation of Sub folder RH with 2 permissions ****

$LRHMainPath = $ListePerm[2]
$ListeRH = “$LRHMainPath\employee”

new-item -type directory -path $ListeRH

$Acl = Get-Acl $ListeRH
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule(“Director”,”FullControl”,”ObjectInherit, ContainerInherit”,”None”,”Allow”)
$Acl.SetAccessRule($Ar)
Set-Acl -path $ListeRH -AclObject $Acl

$Acl = Get-Acl $ListeRH
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule(“Team”,”FullControl”,”ObjectInherit, ContainerInherit”,”None”,”Allow”)
$Acl.SetAccessRule($Ar)
Set-Acl -path $ListeRH -AclObject $Acl

# ****** Popup a message when file create succesfully ****
[System.Windows.Forms.MessageBox]::Show(“File created successfully”,”Message”,1)

$Form.Close()

}

# ***** Start Graphic interface ***
$Form = New-Object System.Windows.Forms.Form
$Form.Text = “Create New Folder”
$Form.Size = New-Object System.Drawing.Size(300,150)
$Form.StartPosition = “CenterScreen”
$Form.Width = 280
$Form.Height = 250

#Show Main Folder name Title
$Font = New-Object System.Drawing.Font(“Times New Roman”,12)
$Form.Font = $Font
$TitleDropDownMenu = New-Object System.Windows.Forms.Label
$TitleDropDownMenu.Location = New-Object System.Drawing.Size(10,10)
$TitleDropDownMenu.Size = New-Object System.Drawing.Size(240,30)
$TitleDropDownMenu.Text = “Main Folder Folder :”
$Form.Controls.Add($TitleDropDownMenu)

#List all main folder in a Drop down menu from a network path
$Folderlist = New-Object System.Windows.Forms.ComboBox
$Folderlist.Location = New-Object System.Drawing.Size(10,40)
$Folderlist.Size = New-Object System.Drawing.Size(240,30)
$Folderlist.DropDownHeight = soft

$FindFolderlist= Get-ChildItem -path “\\AUDAIN-SERVER\AUD Folders” -Filter “* – *”

foreach ($FolderItem in $FindFolderlist) {
$Folderlist.Items.Add($FolderItem)

}
$Form.Controls.Add($Folderlist)

#Show New folder name Title
$Font = New-Object System.Drawing.Font(“Times New Roman”,12)
$Form.Font = $Font
$TitlenewfolderName = New-Object System.Windows.Forms.Label
$TitlenewfolderName.Location = New-Object System.Drawing.Size(10,80)
$TitlenewfolderName.Size = New-Object System.Drawing.Size(240,30)
$TitlenewfolderName.Text = “New Folder Folder :”
$Form.Controls.Add($TitlenewfolderName)

#Show text field for new folder name
$newfolderName = New-Object System.Windows.Forms.textbox
$newfolderName.Multiline = $true
$newfolderName.Location = New-Object System.Drawing.Size(10,110)
$newfolderName.Size = New-Object System.Drawing.Size(240,30)

$Form.Controls.Add($newfolderName)

#Show Create Button
$createButton = New-Object System.Windows.Forms.Button
$createButton.Location = New-Object System.Drawing.Size(10,170)
$createButton.Size = New-Object System.Drawing.Size(100,23)
$createButton.Text = “Create”
$createButton.Add_Click({create})

$Form.Controls.Add($createButton)

#Show QUIT button
$quitButton = New-Object System.Windows.Forms.Button
$quitButton.Location = New-Object System.Drawing.Size(130,170)
$quitButton.Size = New-Object System.Drawing.Size(100,23)
$quitButton.Text = “QUIT”
$QuitButton.Add_Click(({$Form.Close()}))
$Form.Controls.Add($quitButton)

#Show form
$Form.Topmost = $True

[void] $Form.ShowDialog()