Hi,
here is the code:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
$users = Get-ADUser -Filter "*" -SearchBase "OU=DELETE,DC=markaz-uloom,DC=ac,DC=uk" -Properties samaccountname, HomeDirectory, profilepath
#The user who will take ownership
$objUser = New-Object System.Security.Principal.NTAccount("markaz-uloom", "Administrator")
$users | ForEach-Object {
#Get home directory object
$objHomeDirectory = Get-Acl $_.HomeDirectory
#Set home directory object ownership to new user
$objHomeDirectory.SetOwner($objUser)
#delete home directory from server
remove-item $_.HomeDirectory -recurse -force
Write-Host 'Home Directory: '$_.HomeDirectory
#Get profile path object
$objProfilePath = Get-Acl $_.profilepath
#Set profile path object ownership to new user
$objProfilePath.SetOwner($objUser)
#delete profile folder from server, delete line if not needed
remove-item $_.profilepath -recurse -force
Write-Host 'Profile Path: '$_.profilepath `n
#delete user account from AD
#Remove-ADUser -Identity $_.samaccountname -Confirm:$false
#Write-Host 'SamAccountName: '$_.samaccountname
}
Write-Host "Press any key to continue..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
------------------------------------------------------------------------------------------
I have checked 'Home Directory' and 'Profile Path' both exist.