Format drive – the Lazy way
Another day, another quicky. A few new VMs running with additional drives that someone forgot to initialize? Considering RDPing to each of those? Or maybe mmc console (DiskMGMT.MSC) and initializing one by one? What about Honolulu?
No worries, PowerShell will do just fine. I love those quickies that can save you a few clicks here and there!
The RAW meat
So, basically formatting a drive requires these four steps:
- have a disk with raw partition (uninitialized)
- initializing the drive
- creating new partition, and assigning a letter to it
- finally – formatting the drive
So:
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
$driveLetter = (get-disk | Where-Object {$_.PartitionStyle -eq 'RAW'} | Initialize-Disk –PartitionStyle GPT –PassThru | New-Partition –AssignDriveLetter –UseMaximumSize ).DriveLetter | |
Format-Volume –NewFileSystemLabel 'DATA' –FileSystem 'ReFS' –DriveLetter $driveLetter –Force –confirm:$false |
Let’s make it usable
Now, if you’re like me and would like to be able to connect to remote machines with different credentials (LAPS!) and format drives with different labels and file systems – there’s a function for you here:
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
function Format-RemoteDrive { | |
<# | |
.SYNOPSIS | |
Formats all RAW drives on a remote system. | |
.DESCRIPTION | |
Using Invoke-Command will connect to remote system (ComputerName) and will format all RAW drives with given properties | |
.PARAMETER ComputerName | |
Name of a remote system to connect to using Invoke-Command. | |
.PARAMETER NewFileSystemLabel | |
Drive label (i.e. "MyData") for all drives to be formatted. | |
.PARAMETER FileSystem | |
FileSystem to be choosen for drives (ReFS and NTFS available) | |
.PARAMETER Credential | |
Alternate credentials to use to connect to ComputerName | |
.EXAMPLE | |
Format-RemoteDrive -ComputerName SomeServer -NewFileSystemLabel DATA -FileSystem ReFS -Credential (Get-Credential) | |
Will connect to SomeServer using given credentials and will format all RAW drives to ReFS with DATA label | |
#> | |
[CmdletBinding()] | |
[OutputType([void])] | |
Param( | |
[Parameter(Mandatory=$True, | |
ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] | |
[string] | |
$ComputerName, | |
[Parameter(Mandatory=$false, | |
ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] | |
[string] | |
$NewFileSystemLabel, | |
[Parameter(Mandatory=$false, | |
ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)] | |
[ValidateSet('NTFS','ReFS')] | |
[string] | |
$FileSystem, | |
[Parameter(Mandatory=$false, | |
ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] | |
[System.Management.Automation.CredentialAttribute()] | |
$Credential | |
) | |
Process{ | |
Write-Verbose –Message "Processing computer {$ComputerName}" | |
$ConnectProps = @{ | |
ComputerName = $ComputerName | |
} | |
if($PSBoundParameters.ContainsKey('Credential')) { | |
Write-Verbose –Message "Credential {$($Credential.UserName)} will be used to connect to computer {$ComputerName}" | |
$ConnectProps.Credential = $Credential | |
} | |
Write-Verbose –Message "Creating PSSession to computer {$ComputerName}" | |
$pssession = New-PSSession @ConnectProps | |
Write-Verbose –Message "Checking if there are any not initialized disks on {$ComputerName}" | |
$rawDisks = Invoke-Command –Session $pssession –ScriptBlock { | |
get-disk | Where-Object {$_.PartitionStyle -eq 'RAW'} | |
} | |
if($rawDisks) { | |
if($rawDisks.count){ | |
Write-Verbose –Message "Found #{$($rawDisks.Count)} raw disks on {$ComputerName}" | |
} | |
else { | |
Write-Verbose –Message "Found #{1} raw disk on {$ComputerName}" | |
} | |
Write-Verbose –Message "Processing disks on {$ComputerName}" | |
Invoke-Command –Session $pssession –ScriptBlock { | |
$driveLetter = (get-disk | Where-Object {$_.PartitionStyle -eq 'RAW'} | Initialize-Disk –PartitionStyle GPT –PassThru | New-Partition –AssignDriveLetter –UseMaximumSize ).DriveLetter | |
$result = Format-Volume –NewFileSystemLabel $USING:NewFileSystemLabel –FileSystem $USING:FileSystem –DriveLetter $driveLetter –Force –confirm:$false | |
$result | |
} | |
} | |
Write-Verbose –Message "Removing PSSession to Computer {$($pssession.ComputerName)}" | |
$pssession | Remove-PSSession | |
} | |
} |
Or You can grab it as part of PPoSh (Polish PowerShell User Group) Module from GitHub or PowerShell Gallery. There’s more goodies in there.
If you’ve installed it before (Install-Module PPoShTools), just update it (Update-Module PPoShTools).
One thought on “Format Drive. Remotely”