Guide to Automatically Create Partitions on Windows Server Core Using PowerShell
When managing a Windows Server Core installation, you often need to create and manage disk partitions via PowerShell, especially when remote computer management is not available due to WinRM being disabled. This guide will walk you through automatically scanning for new disks and creating partitions on them.
Prerequisites
- Ensure you have added one or more virtual disks to your server, whether it’s VMware or Azure.
- PowerShell access to the server.
Step-by-Step Instructions
Automatically Create Partitions Script
Here’s a PowerShell script that automatically scans for new disks, initializes them, and creates partitions with NTFS formatting:
# Scan for new disks
$newDisks = Get-Disk | Where-Object { $_.PartitionStyle -eq 'RAW' }
if ($newDisks) {
foreach ($disk in $newDisks) {
Write-Host "Found new disk $($disk.Number)"
# Initialize the disk
Initialize-Disk -Number $disk.Number -PartitionStyle GPT
# Create partition and format it
New-Partition -DiskNumber $disk.Number -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "DataDisk$($disk.Number)"
}
}
else {
Write-Host "No new disks found."
}
This script scans for disks with a partition style of ‘RAW’ (indicating uninitialized disks), initializes them with GPT partition style, creates partitions using maximum available size, and formats them as NTFS with labels like “DataDisk1”, “DataDisk2”, etc.
Leave a Reply
Want to join the discussion?Feel free to contribute!