Format NTFS disk with allocation unit size of 64K via PowerShell

In a previous post I showed you how to get the allocation unit size of a Windows disk. In this post I will show you how to set it using PowerShell.

To set the allocation unit size in the GUI, follow the normal steps to format a drive and in the Format Partition box select 64K in the Allocation unit size drop-down per the below:

20160829170230

Provide a Volume Label if desired, then click Next and Finish, after which your drive will be formatted with NTFS with an allocation unit size of 64K.

To do this in PowerShell, run the script below:

$Disk = Get-Disk -Number 1
Set-Disk -InputObject $Disk -IsOffline $false
Initialize-Disk -InputObject $Disk
New-Partition $Disk.Number -UseMaximumSize -DriveLetter E
Format-Volume -DriveLetter E -FileSystem NTFS -AllocationUnitSize 65536 -NewFileSystemLabel DATAFILES -Confirm:$false

The script above will get Disk 1 in Disk Management and initialize it, bring it online, assign it letter E, format it with NTFS and an allocation unit size of 64K and assign it the label DATAFILES.

I found a great script here for performing the same task as above.

References:
Format-Volume
PowerShell Script to Configure SQL Server Storage

Get the allocation unit size of an NTFS partition in Server 2012

If you’re a SQL Server Pro you will know that the volumes the SQL log and datafiles are stored on should be formatted with a 64K cluster size. This is a SQL Server Best practice as stated in this link.

To find out what the allocation unit size is for a drive you would run the command below:

fsutil fsinfo ntfsinfo [your drive]

Running the same command on a test system on the E:\ drive I get the following result:

C:\>fsutil fsinfo ntfsinfo E:
NTFS Volume Serial Number :       0x0c76bbd176bbba32
NTFS Version   :                  3.1
LFS Version    :                  2.0
Number Sectors :                  0x00000000013fe7ff
Total Clusters :                  0x0000000000027fcf
Free Clusters  :                  0x0000000000027d5b
Total Reserved :                  0x0000000000000000
Bytes Per Sector  :               512
Bytes Per Physical Sector :       512
Bytes Per Cluster :               65536
Bytes Per FileRecord Segment    : 1024
Clusters Per FileRecord Segment : 0
Mft Valid Data Length :           0x0000000000010000
Mft Start Lcn  :                  0x000000000000c000
Mft2 Start Lcn :                  0x0000000000000001
Mft Zone Start :                  0x000000000000c000
Mft Zone End   :                  0x000000000000cca0
Resource Manager Identifier :     28DB2529-6D43-11E6-80C7-00505693BAC6

As you can see from the above, the Bytes Per Cluster is equal to 65,536 bytes or 64K.

You would have to run this command for every drive that you want to check. I found this handy piece of PowerShell code that gets just the allocation unit size for all disks in the system:

$wql = "SELECT Label, Blocksize, Name FROM Win32_Volume WHERE FileSystem='NTFS'"
Get-WmiObject -Query $wql -ComputerName '.' | Select-Object Label, Blocksize, Name

The result of which would look similar to this on a system running SQL Server:

20160830081407

The results above show which disks are configured with the default 4K allocation unit size and those configured with 64K which are the SQL disks.

References:
How To Determine Your Hard Disks Cluster Size
SQL Server Best Practices Article
Disk Partition Alignment Best Practices for SQL Server
Get Cluster size for all disks and volumes on a Windows machine using powershell and wmi