Script – Powershell script to remove owership & permission to user from Private folder

If we want to remove the ownership & permission to fired employee from his Private folder

1. Create CSV with username (username should be as same userfolder) like the print screen

2. create this script as you need (Take ownership script)

# Remove Owner from the folder
$rows=Import-Csv "C:\Users\myusername\Desktop\userlist.csv"
foreach ($row in $rows) {
  $UsernameX = $row.UserName
  
$acl = Get-Acl D:\PrivateFolders\AAA_OLD\$usernameX

takeown /f D:\PrivateFolders\AAA_OLD\$usernameX
}

3. Create this script as you need (to remove permission from folder)

# Read in List of Users + Remove Permission to User
$rows=Import-Csv "C:\Users\myusername\Desktop\userlist.csv"
foreach ($row in $rows) {
  $UsernameX = $row.UserName
  
$acl = Get-Acl D:\PrivateFolders\AAA_OLD\$usernameX

$usersid = New-Object System.Security.Principal.Ntaccount ("il\$UsernameX")

$acl.PurgeAccessRules($usersid)

$acl | Set-Acl D:\PrivateFolders\AAA_OLD\$usernameX
}

Enjoy 🙂