Utoljára aktív 1744673383

cleanup-old-file.ps1 Eredeti
1# Define the path to the file or folder
2$path = "C:\Program Files (x86)_1"
3
4# Get the current user's identity
5$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
6
7# Create a security identifier (SID) for the current user
8$currentUserAccount = New-Object System.Security.Principal.NTAccount($currentUser)
9$currentUserSid = $currentUserAccount.Translate([System.Security.Principal.SecurityIdentifier])
10
11# Process each item recursively
12Get-ChildItem -Path $path -Recurse -Force | ForEach-Object {
13 # Get the current ACL of the item
14 $acl = Get-Acl $_.FullName
15
16 # Set the owner to the current user
17 $acl.SetOwner($currentUserSid)
18
19 # Create a new access rule for FullControl
20 $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($currentUser, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
21
22 # Add the new access rule to the ACL
23 $acl.AddAccessRule($rule)
24
25 # Apply the modified ACL (including new owner and permissions) to the item
26 Set-Acl -Path $_.FullName -AclObject $acl
27}
28
29Write-Host "Ownership and permissions have been updated successfully."
30Remove-Item -Path $filePath -Recurse -Force