We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

Delete/Purge user profile folder

February 18, 2021 Views: 4176

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.

Edit Remove
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.

Edit Remove
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
}

Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers