Script repository

Move home folder with locked files

Updated on: Jan 18, 2026, Views: 2993

Folders and profiles

The script moves a user home folder to another location. Unlike the built-in Move home folder action, it does not stop when unable to move a folder or file, and copies it instead. All locked items will be listed in the Execution log. To execute the script, create a business rule, custom command or scheduled task configured for the User object type.

In the script, the $destinationPath variable specifies a path to the folder to move user home folder to.

$destinationPath = "\\server\share\%username%" # TODO: modify me

function DeleteObject ($objectPath)
{
    try
    {
        Remove-Item $objectPath -Force -ErrorAction Stop
    }
    catch
    {
        $Context.LogMessage("Error deleting the home directory of the user. Could not delete file or folder '$objectPath'. Error message: " + $_.Exception.Message, "Warning")
    }
}

function GetChildItems ($objectPath)
{
    $object = Get-Item $objectPath -Force
    if ($object -isnot [System.IO.DirectoryInfo])
    {
        DeleteObject $object.FullName
        return
    }
    
    $childItems = Get-ChildItem $object.FullName -Force
    if($childItems -eq $NULL)
    {
        DeleteObject $object.FullName
        return
    }
    
    foreach ($item in $childItems)
    {
        GetChildItems $item.FullName
    }
    
    DeleteObject $object.FullName
}

try
{
    Copy-Item -Recurse -Path "%homeDirectory%" -Destination "$destinationPath" -Force -ErrorAction Stop
}
catch
{
    $Context.LogMessage($_.Exception.Message, "Warning")
    return
}

# Update user.
$Context.TargetObject.Put("homeDirectory", $destinationPath)
$Context.TargetObject.SetInfo()

# Remove old folder.
GetChildItems "%homeDirectory%"

Comments 0

You must be signed in to comment.

    Got questions?

    Support Questions & Answers

    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.