Format Drive. Remotely

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:

  1. have a disk with raw partition (uninitialized)
  2. initializing the drive
  3. creating new partition, and assigning a letter to it
  4. finally – formatting the drive

So:


$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:


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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s