In the backup method, I got some illegal character errors for these lines:
$sourceItemPath = Join-Path -Path $sourcePath -ChildPath $item.Name
Copy-Item -LiteralPath $sourceItemPath -Destination $destItemPath -Recurse -Force
But, even after resizing the partition several times, up to 2 GB, I still get the same error on some machines. I have noticed that it is only happening on Server 2022, 21H2, 20348.2322 when the Windows RE partition was already over 500MB. On computers with the same build and a smaller recovery partition, the install went fine many weeks ago.
Thank you for the script though, it was good to see how to do these things, and query the free space within unmounted partitions.