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

Move home folder with locked files

July 05, 2021 Views: 2037

The script moves a user's 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 are listed in the Execution Log.

To add the script to your business rule, custom command or scheduled task, use the Run a program or PowerShell script action.

Parameter:

  • $destinationPath - Specifies a path where the script should move a user's home fodler. You can use value references (e.g. %username%) in the path. They will be replaced with corresponding property values of the user account.
Edit Remove
PowerShell
$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 # Exit script
}

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

# Remove old folder
GetChildItems "%homeDirectory%"
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers