Im trying to make a script that will walk through a dir and check if the file exists or not. if it does exists it needs to have only permissions for the user and the admin group. If it doesnt exist the folder needs to be created. Here is what i am working with so far.. not an experienced code writer at all. I know the creating a new folder works because i pulled that from my user creation script. i just need to go fix all the wrongly permissioned/not existing ones for users .
$users = Get-AdUser -filter * -searchbase "OU=SAMPLEOU, DC=SAMPLE, DC=Com"
$Targetdir = "\\SAMPLE\Users$\$username"
Foreach ($user in $users)
{
if(Test-Path -Path $Targetdir){
icacls "\\SAMPLE\Users$\$($username)" /inheritance:d
icacls "\\SAMPLE\Users$\$($username)" /grant "SAMPLE\$($username):(OI)(CI)M"
icacls "\\SAMPLE\Users$\$($username)" /remove $domain /T
}
Elseif (!(Test-Path -Path $Targetdir)){
mkdir "\\SAMPLE\Users$\$($username)"
icacls "\\SAMPLE\Users$\$($username)" /inheritance:d
icacls "\\SAMPLE\Users$\$($username)" /grant "SAMPLE\$($username):(OI)(CI)M"
icacls "\\SAMPLE\Users$\$($username)" /remove $domain /T
}
}