This script deletes a user's profile specified in the Profile Path attribute. To delete a user's profile as a part of a business rule, scheduled task, or custom command, you need to use the Run a program or PowerShell script action that executes the script.
PowerShell
# Get profile path
try
{
$profilePath = $Context.TargetObject.Get("profilePath")
}
catch
{
return
}
# Check whether the profile folder exists
if(!(Test-Path -Path $profilePath))
{
$Context.LogMessage("Incorrect profile path: $profilePath", "Error")
return
}
# Delete the profile folder
Remove-Item -Path $profilePath -Force -Recurse
If you want only to purge the content of a user's profile folder without deleting it, you can use the following version of the script.
PowerShell
# Get profile path
try
{
$profilePath = $Context.TargetObject.Get("profilePath")
}
catch
{
$Context.LogMessage("No profile path specified for the user", "Warning")
return
}
# Delete the contents of the profile folder
$childItem = Get-ChildItem -Path $profilePath -Force
if($childItem -ne $NULL)
{
$childItem | Remove-Item -Force -Recurse
}