Created
November 10, 2016 19:34
-
-
Save spuder/50b722400097efb3854de7408065a8af to your computer and use it in GitHub Desktop.
Formats empty volumes on windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Write-Host "Initializing and formatting raw disks" | |
$disks = Get-Disk | where partitionstyle -eq 'raw' | |
## start at F: because D: is reserved in Azure and sometimes E: shows up as a CD drive in Azure | |
$letters = New-Object System.Collections.ArrayList | |
$letters.AddRange( ('F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z') ) | |
Function AvailableVolumes() { | |
$currentDrives = get-volume | |
ForEach ($v in $currentDrives) { | |
if ($letters -contains $v.DriveLetter.ToString()) { | |
Write-Host "Drive letter $($v.DriveLetter) is taken, moving to next letter" | |
$letters.Remove($v.DriveLetter.ToString()) | |
} | |
} | |
} | |
ForEach ($d in $disks) { | |
AvailableVolumes | |
$driveLetter = $letters[0] | |
Write-Host "Creating volume $($driveLetter)" | |
$d | Initialize-Disk -PartitionStyle GPT -PassThru | New-Partition -DriveLetter $driveLetter -UseMaximumSize | |
# Prevent error ' Cannot perform the requested operation while the drive is read only' | |
Start-Sleep 1 | |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "datadisk" -DriveLetter $driveLetter -Confirm:$false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Part of the logic comes from here:
https://www.opsgility.com/blog/windows-azure-powershell-reference-guide/format-disks-on-boot-with-windows-azure-virtual-machines-and-powershell/