Creating Background Jobs for Move-Item if folder is over 1GB

I’m trying to create a Employee Separation script that the folks in the Help desk can run rather then having to do each one manually at the admin level.

This part is in the Begin block.

$COAADuser = Get-ADUser -identity $Username –properties * -server COADC1.COACD.ORG
$ATXADUser = Get-ADUser -identity $Username –properties * -server ATXDC1.ATXAD.ORG

What I have is this in the Process block of my advanced function.

Start-Job -Name HomeDriveCopy -ScriptBlock {Copy-Item -Path $args[0] -Destination $args[1] -Recurse -Verbose} -ArgumentList $COAADuser.HomeDirectory,$pathvalue
Sleep 5
$HomeCopyJob = Get-Job -Name HomeDriveCopy

while($HomeCopyJob.State -ne ‘Completed’)
sleep 10

if ($HomeCopyJob.State -eq ‘Completed’) {continue}



What I’d like to do is have it setup so that if the users home folder is bigger then a 1gb then to have it run as a background job and once the job is completed to export the statistics for the move-item job to the folder it’s moved to.

I’d also like to be able to filter any .mp3, .wav, .wma, and move to a separate folder called Audio files and exclude things like the desktop.ini files and empty root folders.

Ideally I’d like to have it when the Move job was complete to give the Manager who’s set on the users AD object full rights to the share we moved the folders and files to and then create a shortcut in the managers H:\ folder for the directory with the information.

Here’s what I have however I haven’t had much success with it. This was from googling how to create a shortcut with powershell. I didn’t know if something like new-item -path $managerhome and such would work.

This is supposed to run in the end part of the script if the copy state is complete and give the manager rights to the folder and all items, then create a shortcut to the $pathvalue in the managers H:\ drive.

if($HomeCopyJob.State -eq ‘Completed’){

 $HomeFolders = Get-ChildItem $pathvalue -Directory

    foreach ($HomeFolder in $HomeFolders) {

        $Path = $HomeFolder.FullName
        $Acl = (Get-Item $Path).GetAccessControl('Access')

         $COAManager = $COAADuser.Manager
         $ATXManager = $ATXADUser.Manager 

        $COAman = (Get-ADUser -Identity $COAManager -Properties * -Server COADC1.COACD.ORG)
        $ATXman = (Get-ADUser -Identity $ATXManager -Properties * -Server ATXDC1.ATXAD.ORG)

        $COAMANSAM = $COAMan.SamAccountName
        $ATXMANSAM = $ATXman.SamAccountName

        $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($COAMANSAM, 'Modify',                 'ContainerInherit,ObjectInherit', 'None', 'Allow')
        Set-Acl -path $Path -AclObject $Acl


$managerHome= $COAman.HomeDirectory
  #Creates Shortcut in Managers H:\ drive Folder
$WshShell=New-Object -comObject WScript.Shell


Sounds like the typical “pick the right tool for the job” question. Have you considered using robocopy? It can quickly calculate the homedrive size and copy with a filetype blacklist,
e.g. “robocopy /xf *.jpg”.

Thanks for the suggestion. I’ll give that a try just need to figure out the syntax as lat time I tired I got an error regarding permissions.

Thanks Sebastian Neumann. Here’s what I updated the script to use instead.

Robocopy.exe $COAADuser.HomeDirectory $Homedir /s /e /move /eta /R:5 /w:60 /xf *.mp3 desktop.ini AlbumArtSmall.jpg Thumbs.db Folder.jpg AlbumArt /log:$homecopylog

Now the only issue is that I’d like for it to not keep the exclude files in the old Home drive, but I have a added it so that if the home drive still exist to tell the Help Desk agent running this script they need to clean up the users home drive.

Thanks again!