Remove partition as part of Windows 10 in-place upgrade with PowerShell

The Windows 10 in-place upgrade ConfigMgr Task Sequence is going to make the migration to Windows 10 relatively pain free and quick. You can have an upgraded O/S running the latest release within the space of an hour. With support for upgrade for Windows 7 & 8 it makes sense to invest time and effort into this method for rolling out the soon to be released O/S.

The in-place upgrade Task Sequence, by default, will only do as its name suggests - perform an in-place upgrade. You may wish to use the migration exercise to perform some other tasks to clean up your environment. One of these tasks might be to clean up the local drives i.e. get rid of that pesky D: drive that someone created in your image back in the day for no reason whatsoever!

ReconfigDisk-001

With this in mind I have created a small script that can be added to the in-place upgrade Task Sequence. If you need details on the in-place upgrade Task Sequence, I recommend that you check out Nickolaj Andersen's blog post on the subject.

The complete script can be downloaded from the Technet Galleries here

NOTE - At present the Task Sequence change does not take into account drives with BitLocker enabled. If a drive has BitLocker enabled ensure that this is disabled prior to running the script as disk configuration changes are made.

Script Breakdown

Before I break down the script a quick caveat. This script copies data from your extra drive and then removes the drive partition. If the data copy does not complete successfully for whatever reason then you could possibly lose data from the drive.

A WMI query takes place to filter for drive letter D: - if you are using a different letter then this can be modified.

$ExtraDrive = Get-WmiObject win32_logicaldisk -Filter "DeviceID ='D:'"

There is then an if statement that runs only if the drive letter D: exists - again this can be modified

if ($ExtraDrive.DeviceID -eq 'D:') {

Next the data is copied from the D: drive to root of the C:\ with an xcopy command

xcopy $ExtraDrive.DeviceID c:\ /s /y > $env:temp\DataCopy.log

A variable called $DriveLetter is created. This variable contains the drive letter with the colon trimmed so it can be used in the next line of code - the Remove-Partition command.

$DriveLetter = $ExtraDrive.DeviceID.Trim(":")

The Remove-Partition command removes the D: drive and suppresses confirmation of this task.

Remove-Partition -DriveLetter $DriveLetter -Confirm:$false

Next a query is made for the maximum supported size of the C: drive and this is stored in the variable $MaxSize

$MaxSize = (Get-PartitionSupportedSize -DriveLetter C).sizeMax

A Resize-Partition command is issued for the C: drive to use the maximum size available, stored in the $MaxSize variable.

Resize-Partition -DriveLetter C -Size $MaxSize

Finally the if statement is closed off and an else is issued that can write back to the smsts.log file with details if the D: drive does not exist.

}

else { write-host "No drive with that letter exists"
}

Adding to the Task Sequence

Copy the ReconfigureDisk.ps1 file into the Windows vNext Upgrade Scripts folder on the source content then run an Update Distribution Points for that package.

DriveResize-002

Edit the Windows vNext Upgrade and add in a Run PowerShell Script step in the Post-Processing section before the Cleanup task. Load the Windows vNext Upgrade Scripts package and enter ReconfigureDisk.ps1 as the script name. Set the execution policy to Bypass.

DriveResize-003

Once the in-place upgrade has been performed the D: drive will have been deleted and the C: drive resized.

ReconfigDisk-002

and a folder previously located on the D: Drive has been copied over.

ReconfigDisk-003

Since the xcopy command file logged the copy, I can view the completed log file DataCopy.log in the c:\windows\temp folder.

ReconfigDisk-004

The complete script can be downloaded from the Technet Galleries here