0 votes

I would like to generate a script that would copy the contents of a users home folder to their managers home folder when the deprovisioning script is fired off. my preference would be that it create a subfolder with the users name in the managers home folder for example

if not exist, create an "Old Users" folder in managers home directory
copy users home folder to managerhomefolder\old users\deprovisionedusername\contents

how would I do this in a script?

by (40 points)

1 Answer

0 votes
by (216k points)

Hello,

You can use the following script to copy a user's home folder to his manager's home folder.

function CopyDirectory($source, $target)
{
    # If the destination directory does not exist, create it.
    if (-not([System.IO.Directory]::Exists($target)))
    {
        [System.IO.Directory]::CreateDirectory($target);
    }

    $sourceDir = New-Object "System.IO.DirectoryInfo" $source;
    # Copy files
    $files = $sourceDir.GetFiles();
    foreach ($file in $files)
    {
        # Create the path to the new copy of the file.
        $temppath = [System.IO.Path]::Combine($target, $file.Name);

        $file.CopyTo($temppath, $false);
    }

    # Copy sub-directories    
    $subDirs = $sourceDir.GetDirectories();
    foreach ($subDir in $subDirs)
    {
        # Create the subdirectory.
        $temppath = [System.IO.Path]::Combine($target, $subDir.Name);

        # Copy the subdirectories.
        CopyDirectory $subDir.FullName $temppath;
    }
}

# Get the user's home folder
$user = $Context.TargetObject
try
{
    $userHomeDirectory = $user.Get("homeDirectory")
}
catch [System.Runtime.InteropServices.COMException]
{
    # The user doesn't have home directory
    return;
}
if (-not([System.IO.Directory]::Exists($userHomeDirectory)))
{
    $Context.LogMessage("The user's home directory doesn't exist.", "Warning")
    return;
}

# Get manager's home folder
try
{
    $managerDN = $user.Get("manager")
}
catch [System.Runtime.InteropServices.COMException]
{
    $Context.LogMessage("The user doesn't have any manager specified.", "Warning")
    return;
}
$manager = $Context.BindToObjectByDN($managerDN)
try
{
    $managerHomeDirectory = $manager.Get("homeDirectory")
}
catch [System.Runtime.InteropServices.COMException]
{
    $Context.LogMessage("The user's manager doesn't have home directory.", "Warning")
    return;
}
$targetDirectory = $managerHomeDirectory + "\Old Users\%username%"

# Copy user's home folder
CopyDirectory $userHomeDirectory $targetDirectory

Related questions

0 votes
1 answer

I am having an issue with home folder moves between servers. When I move a folder between servers, using Adaxes, the user permissions are lost. I know that this is expected ... to re-assign the user permissions so they can access their home folder again :?:

asked Sep 23, 2014 by rmedeiros (380 points)
0 votes
1 answer

Hello Adaxes Support, is there any way you could help me with a script that changes the permissions on a users home folder? I need to remove the permission to delete the folder and add the permission to delete subfolders and files.

asked Aug 1 by dominik.stawny (280 points)
0 votes
1 answer

seting up a scheduled task to move users to thier correct OU. For some we can do this based on employee type and direct to a specific OU. For most of our users we will have to script this to move to the manager's OU.

asked Apr 12, 2023 by mightycabal (1.0k points)
0 votes
1 answer

Good Morning, I've been working through some of my processes and I'm not looking to make sure the deletion of Home directories (both remote and standard) as well as ... for user deletion. If there are any questions or clarification needed, please let me know.

asked Oct 16, 2015 by jtop (700 points)
0 votes
1 answer

Hi folks, I already have a great script (thank you) that monitors our HR system for adds/removes/changes of our staff and reflects those changes in AD via scheduled ... way of doing this? Could I possibly incorporate it into the existing script? Thanks Corey

asked Dec 11, 2014 by ckemp (170 points)
3,548 questions
3,238 answers
8,232 comments
547,812 users