Clear-Host $MyLine = [string] $PathToProcess = [string] $PathFolderForCACLS = [string] $CheminFichier = [string] $AdministratorLogin = [string] $AdministratorLogin = "Administrators" function Get-ScriptDirectory { $Invocation = (Get-Variable MyInvocation -Scope 1).Value $ScriptFolderPath = Split-Path $Invocation.MyCommand.Path return $ScriptFolderPath } #$PathToProcess = "C:\Chemin à traiter" $PathToProcess = Get-ScriptDirectory $NomFichier = "BatReinitDroits.bat" #$RepertoireFichier = "C:\Repertoire pour ecrire le BAT resultant" $RepertoireFichier = Get-ScriptDirectory $CheminFichier = "$RepertoireFichier\$NomFichier" Write-Host $CheminFichier $MonFichier = New-Item -type file $CheminFichier -Force $MonFolder = Get-ChildItem -Path $PathToProcess | Where-Object {$_.PSIsContainer} foreach ($MySubFolder in $MonFolder) { $PathFolderForCACLS = "`""+$PathToProcess+"\"+$MySubFolder.name+"`"" $MyLine = "TAKEOWN /F $PathFolderForCACLS /a /r /d Y" add-content $MonFichier $MyLine $MyLine = "iCACLS $PathFolderForCACLS /reset /T /C /Q" add-content $MonFichier $MyLine $MyLine = "iCACLS $PathFolderForCACLS /Grant `""+$MySubFolder.name+"`":(OI)(CI)F /C /Q" add-content $MonFichier $MyLine $MyLine = "REM iCACLS $PathFolderForCACLS /Grant `""+$AdministratorLogin+"`":(OI)(CI)F /C /Q" add-content $MonFichier $MyLine $MyLine = "iCACLS $PathFolderForCACLS /setowner `""+$MySubFolder.name+"`" /T /C /Q" add-content $MonFichier $MyLine }