Last active 1744673383

elicro revised this gist 1744673383. Go to revision

1 file changed, 0 insertions, 0 deletions

cleanup-old-file.psq renamed to cleanup-old-file.ps1

File renamed without changes

elicro revised this gist 1744673376. Go to revision

1 file changed, 30 insertions

cleanup-old-file.psq(file created)

@@ -0,0 +1,30 @@
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
12 + Get-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 +
29 + Write-Host "Ownership and permissions have been updated successfully."
30 + Remove-Item -Path $filePath -Recurse -Force
Newer Older