Skip to content

Instantly share code, notes, and snippets.

@JimMoyle
Created April 5, 2022 22:14
Show Gist options
  • Save JimMoyle/6bb3057bdfa5a645e6d426086403ca18 to your computer and use it in GitHub Desktop.
Save JimMoyle/6bb3057bdfa5a645e6d426086403ca18 to your computer and use it in GitHub Desktop.
#Requires -RunAsAdministrator
#Requires -Modules 'Hyper-V'
# Once this has been run once, you will be able to use the Invokle-ShrinkFslDisk tool to get max shrink from all disks, without having to copy.
# Required Services = 'defragsvc', 'vds', 'smphost' ! This script will fail if you don't have these services running !
# Change the source and target path as needed
# Run from a VM with FSLogix installed
# Does not need to be run on a FileServer
$sourcePath = 'D:\Test VHDs\Profile_User2.vhdx'
$targetPath = 'D:\Test VHDs\New.vhdx'
# Check FSlogix is installed
$exePath = Join-Path $Env:ProgramFiles 'FSLogix\Apps\frx.exe'
If (-not (Test-Path $exePath)){
Write-Error 'frx.exe not found at path'
return
}
#create very small disk
& $exePath create-vhd -fileName $targetPath -size-mbs 200
#resize disk to same size as s ource disk ( this means you can shirink it later )
Resize-VHD -Path $targetPath -SizeBytes ((Get-diskimage -ImagePath $sourcePath).Size)
#Need to also resize partition inside disk so mount it first
$mountInfo = Mount-VHD -Path $targetPath -Passthru
#Getpartition inormation so we can change partition size
$partition = Get-Partition -DiskNumber $mountInfo.DiskNumber
#Change partition size to the maxmimum amount
Resize-Partition -Size ($mountInfo | Get-PartitionSupportedSize).Sizemax -DiskNumber $mountInfo.DiskNumber -PartitionNumber $partition.PartitionNumber
#Clean up by dismounting the disk
$mountInfo | Dismount-VHD
#Copy contents of old disk to new disk
& $exePath migrate-vhd -src $sourcePath -dest $targetPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment