Skip to content

Instantly share code, notes, and snippets.

@kewalaka
Last active April 2, 2021 00:06
Show Gist options
  • Select an option

  • Save kewalaka/663748a99c6e46bb32e0841136a797be to your computer and use it in GitHub Desktop.

Select an option

Save kewalaka/663748a99c6e46bb32e0841136a797be to your computer and use it in GitHub Desktop.
This will modify the C: disk volume, halving it to create space for another partition D:, and enable BitLocker
<#
.Synopsis
Divides the Partition hosting C: into two partitions
.DESCRIPTION
This script;
- moves the optical drive to Z:
- divides c: into two partitions
- creates partition & formats D: with the label 'Local Data'
- enables BitLocker for D: (assumes that the recovery key is stored in AD)
.EXAMPLE
There are no parameters:
.\Add-ExtraDiskPartition.ps1
.NOTES
Author: Stu Mace <stum@datacom.co.nz>
#>
# change CD to Z:, DriveType=5 indicates an optical drive, it is assumed there is at most only one
# this is required because if present the optical drive will typically occupy D:
try
{
Get-CimInstance -Class Win32_Volume -Filter 'DriveType=5' |
Select-Object -First 1 |
Set-CimInstance -Arguments @{DriveLetter='Z:'}
}
catch
{
Write-Output 'No optical drive found'
}
# get the disk number of the C: drive, required later
$CDiskNumber = (Get-Partition -DriveLetter C).DiskNumber
# get the total capacity of the C: drive
$maxDiskSize = (Get-Disk | Where Number -eq $CDiskNumber).Size
# check that the drive is health before doing any partitioning
if ((Get-Volume -DriveLetter C).HealthStatus -eq 'Healthy')
{
# resize the partition by half
$partitionNumber = (Get-Partition -DriveLetter C).PartitionNumber
$maxPartitionSize = (Get-PartitionSupportedSize -DriveLetter C).SizeMax
# if the current partition is the same size as disk, a resize is required
# a margin of error of 10GB is used to account for system reserved partitions
if (($maxDiskSize - $maxPartitionSize) -lt (10 * 1024 * 1024 * 1024))
{
# resize the C partition (make half the size)
Resize-Partition -DriveLetter C -Size ($maxPartitionSize / 2 )
# create & format the new partition without prompting, based on:
# https://stackoverflow.com/questions/42620986/formatting-a-disk-using-powershell-without-prompting-for-confirmation
$partition = New-Partition -DiskNumber $CDiskNumber -UseMaximumSize
$partition | Format-Volume -NewFileSystemLabel 'Local Data' -Confirm:$false
$partition | Set-Partition -NewDriveLetter D
}
# enable bitlocker if protection is not on
if ($null -eq (manage-bde.exe -status d: | select-string 'Protection On'))
{
manage-bde -on D: -recoverypassword -skiphardwaretest
}
}
else
{
Write-Warning ("C: drive status reports not healthy, status: {0}" -f (Get-Volume -DriveLetter C).HealthStatus)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment