-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet_Access_Folder_Recurse.ps1
More file actions
33 lines (26 loc) · 1.19 KB
/
Get_Access_Folder_Recurse.ps1
File metadata and controls
33 lines (26 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Get_Access_Folder_Recurse.ps1
# ---------------------------------------------
# PowerShell script to take ownership and reset permissions recursively
# Prompts for a folder path, then:
# - Takes ownership of all files and folders
# - Grants full control to the current user
# - Removes all other permissions
# ---------------------------------------------
# Prompt for folder path
$folderPath = Read-Host "Enter the full path to the folder you want to unlock"
if (-not (Test-Path $folderPath)) {
Write-Host "Path does not exist: $folderPath" -ForegroundColor Red
exit 1
}
# Get current user
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
Write-Host "Taking ownership of all files and folders in $folderPath..."
# Take ownership recursively
icacls "$folderPath" /setowner "$currentUser" /T /C
Write-Host "Granting full control to $currentUser..."
icacls "$folderPath" /grant "$($currentUser):(OI)(CI)F" /T /C
Write-Host "Removing all other permissions..."
icacls "$folderPath" /reset /T /C
icacls "$folderPath" /grant "$($currentUser):(OI)(CI)F" /T /C
Write-Host "Done. $currentUser now has full control over $folderPath and all its contents." -ForegroundColor Green
pause