Guide to Automatically Create Partitions on Windows Server Core Using PowerShell

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.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *