Check whether disk partitions contain the correct 4KB alignment by using PowerShell


Download Windows Server 2012

Introduction

This script can be used to check whether user’s disk partitions contain the correct 4KB alignment. Over the next few years, data storage will transition from physical format of hard disk drives that are in 512-byte sectors to 4,096-byte sectors (also known as 4K or 4KB sectors) hard disk drives. This change includes increases in storage density and reliability.  Many customers have to know whether their disk partitions have the non-4kb alignment issues which can cause incompatibility issues with existing software (including operating systems and applications).

Scenarios

We can use Test-OSC4kAligned function to check whether user’s disk partitions contain the correct 4kb alignment.

Script

This script contains the Format-OSCResult and Test-OSC4kAligned functions.  Format-OSCResult is an internal function to provide a formatter operation for Test-OSC4kAligned. You can use them in following ways:

Method 1:

  1. Download the script and copy it to your computer.
  2. Open the script file by using Notepad or any other script editors.
  3. Scroll down to the end of the script file, and then add the example command which you want to run.
  4. Save the file, and then run the script in PowerShell console.

Method 2:

  1. Rename scriptname.ps1 to scriptname.psm1 (PowerShell Module file)
  2. Run Import-Module cmdlet to import this module file.
    Import-Module filepath\scriptname.psm1


Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

PowerShell
Edit|Remove
#Get disk drives 
$diskDrives = Get-WmiObject -Class Win32_DiskDrive 
  
#foreach disks 
foreach($diskDrive in $diskDrives) 
{ 
    #Convert disk drive to disk partition 
    $deviceID = $diskDrive.DeviceID -replace "\\","\\" 
    $diskPartitions = Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$deviceID`"} ` 
    WHERE AssocClass=Win32_DiskDriveToDiskPartition" 
     
    foreach($diskPartition in $diskPartitions) 
    { 
        $is4kAligned = ($diskPartition.StartingOffset % 4096) -eq 0 
        $name = $diskPartition.Name 
        $partitionSize = "{0:0.00}" -f ($diskPartition.Size /1GB) 
        $description = $diskPartition.Description 
         
        #Convert disk partition to logical disk 
        $query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($diskPartition.DeviceID)`"} ` 
        WHERE AssocClass=Win32_LogicalDiskToPartition"  
        $logicalDisks = Get-WmiObject -Query $query 
         
        foreach($logicalDisk in $logicalDisks) 
        { 
            $deviceID = $logicalDisk.DeviceID 
            $diskSize = "{0:0.00}" -f ($logicalDisk.Size /1GB) 
            $deviceInfoList += Format-OSCResult $is4kAligned $name $deviceID $diskSize $partitionSize $description 
        } 
    } 
}

Examples

Example 1: Display help about Test-OSC4kAligned
Command:
Get-Help Test-OSC4kAligned
Screenshot:

Example 2: Check if disk partitions are 4kb alignment
Command:
Test-OSC4kAligned
Screenshot:

Prerequisite

Windows PowerShell 2.0

Additional Resources

Technical Resources:
http://support.microsoft.com/kb/2510009