Change CL on folder in parallel way

I have a folder D:\Volumes which contains Archivefolder one per year. In every Archivfolder we have hundreds of subfolders with millions of files.

From time to time we need to clone the whole Folder which is done by NetApp Snapshot clone. With this clone I have to remove the original owner and set a new owner and new access rights recursively.

Currently I am doing this using a powershell script from top to bottom, but this is taking days to finish.

Measure-Command {
	$Mainfolder = "D:\Volumes"
	$folders = Get-ChildItem $Mainfolder
	$OldAccount = "domain\user1"
	$NewAccount = "domain\user2"
	Try
	{
		$Processes = @()
		Write-Host `t  Set New File Owner to $NewAccount -ForegroundColor Green
		$Processes += Start-Process icacls -WindowStyle Hidden -ArgumentList "$Mainfolder /setowner $NewAccount /T /C" -PassThru
		Write-Host `t  Give new Admin owner $NewAccount Full Access to new files and folders and remove old Adminowner $OldAccount -ForegroundColor Green
		$Processes += Start-Process icacls -WindowStyle Hidden -ArgumentList "$Mainfolder /grant ${NewAccount}:(OI)(CI)F /remove:g $OldAccount /C" -PassThru
		#Now that all processes/jobs have been started, let's wait for them (first check if there was any subfile/subfolder)
		#Wait for $Job
		Wait-Process -Id $Processes.Id -ErrorAction SilentlyContinue
		Write-Host "The script has completed resetting permissions under $($Mainfolder)."
	}
	Catch
	{
		$ErrorMessage = $_.Exception.Message
		Throw "There was an error during the script: $($ErrorMessage)"
	}
}

My idea would be to set the variables only on the top folder, but then all subfoldes are processed simultaniously which would take only some hours. How can I archive this?

We are using Windows Server 2019.

Benjamin,
Welcome to the forum. :wave:t3:

The most straight forward way would probably be to install the newest version of PowerShell core (at the moment version 7.5) and use its cmdlet Foreach-Object with the parameter -Parallel

Here you may start reading about it: