Script Repository


Delete/Purge user profile folder

September 04, 2015
1202

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 )
No results found.
Leave a comment