Invoke-sqmFormatDrive64K Filesystem

Checks an NTFS drive for the SQL Server-recommended 64 KB (65536 byte) allocation unit size. If the cluster size differs, the function backs up all data with robocopy, formats the volume, restores the data, and cleans up the backup — all in one auditable workflow. Returns AlreadyOK without making changes if the drive is already correctly formatted.

Execution Flow

START Step 1 — Safety checks Drive C: → hard error | BackupPath not on C: → error Admin rights check | Get-Volume: drive must exist and be NTFS Get-Partition: must have exactly one partition → any check fails → Status=Error; return fsutil fsinfo ntfsinfo → 'Bytes Per Cluster' | fallback: Get-Volume.AllocationUnitSize PreviousClusterSize stored in result object Already 65536 bytes? Status=AlreadyOK; return Get-Process | check .Modules for files on drive → any match → Status=InUse; return Exception in process check → log WARNING and continue Get-ChildItem drive has data? robocopy /E /COPYALL /R:3 /W:5 → backup to $BackupPath\{Letter}_{ts} ExitCode ≤ 3 = OK | ≥ 4 → Status=Error; return -Force or ShouldProcess? Status=Error (cancelled); return Format-Volume -AllocationUnitSize 65536 -FileSystem NTFS -NewFileSystemLabel $label -Force | Restore drive letter If data was backed up: robocopy restore backup → drive | ExitCode ≤ 3 → DataRestored=true Restore OK → Remove-Item backup on C: (BackupCleanedUp=true) | Restore fail → WARNING, backup stays Return PSCustomObject { Status=Formatted, DriveLetter, PreviousClusterSize, NewClusterSize, DataBackedUp, DataRestored, BackupCleanedUp } DONE

Synopsis

Drive C: is permanently prohibited — a hard-coded guard prevents accidental formatting. BackupPath must reside on C:. The cluster size is read first via fsutil fsinfo ntfsinfo (with fallback to Get-Volume.AllocationUnitSize). If the drive is already at 65536 bytes, the function returns AlreadyOK without any changes. The in-use check scans Get-Process module file paths and returns InUse if any process has open file handles on the drive. robocopy uses flags /E /COPYALL /R:3 /W:5 /NP /LOG+; exit codes 0–3 are treated as success.

No dbatools required. Requires local administrator rights. Robocopy is a standard Windows component. -WhatIf returns Status=WhatIf without touching any data.
All data on the drive is destroyed during formatting. The robocopy backup mitigates this risk, but if the restore fails the backup remains under -BackupPath on C: for manual recovery. Never run against a drive actively used by a running SQL Server instance.

Syntax

Invoke-sqmFormatDrive64K
    -DriveLetter <String>           # required; single letter A-Z (not C)
    [-BackupPath <String>]          # default: C:\System\DriveBackup (must be on C:)
    [-Force]                         # skip confirmation prompt
    [-WhatIf] [-Confirm]

Parameters

ParameterTypeDefaultDescription
-DriveLetterStringRequired. Single drive letter A–Z. C is explicitly prohibited (ValidatePattern enforced).
-BackupPathStringC:\System\DriveBackupTemporary backup path on C: for data before formatting. Must start with C:\.
-ForceSwitch$falseSkip the interactive confirmation prompt before formatting.

Return Value

PropertyDescription
DriveLetterTarget drive letter.
LabelDrive label (preserved through format).
PreviousClusterSizeAllocation unit size in bytes before the operation.
NewClusterSizeAllocation unit size in bytes after the operation (65536 on success).
DataBackedUp$true if robocopy backup completed successfully.
BackupFolderPath of the backup folder on C: ($null if no data was present).
DataRestored$true if robocopy restore completed successfully.
BackupCleanedUp$true if the backup on C: was deleted after successful restore.
StatusAlreadyOK — no changes; Formatted — success; InUse — drive in use; Error — any failure; WhatIf — dry run.
MessageHuman-readable result with reboot/restore warnings where applicable.

Examples

# Check drive D: and format if needed (with confirmation prompt)
Invoke-sqmFormatDrive64K -DriveLetter 'D'

# Dry run — shows what would happen, no data changed
Invoke-sqmFormatDrive64K -DriveLetter 'E' -WhatIf

# Format without confirmation, custom backup path
Invoke-sqmFormatDrive64K -DriveLetter 'F' `
    -BackupPath 'C:\Temp\DriveBackup' -Force

# Check result
$r = Invoke-sqmFormatDrive64K -DriveLetter 'D' -Force
Write-Host "$($r.Status): $($r.PreviousClusterSize) → $($r.NewClusterSize) bytes | Restored: $($r.DataRestored)"