Using Powershell to initialize, partition and format a new disk

Powershell is quite handy when remotely managing servers. Especially if they are core-servers. To gain access to a server remotely (rather than logging into it directly you can user the command:

[powershell]Enter-PSSession [/powershell]

This will gain you access to the server and to list the available disks type:

[powershell]Get-Disk[/powershell]

And something similar to the output below will show

Number Friendly Name Operationa Total Size Partition lStatus Style
—— ————- ———- ———- ———
2 SanDisk SDSSDH2256G Online 238.47 GB GPT
6 Microsoft Storage Space Device Online 930 GB GPT
0 WDC WD1200BEVS-22UST0 Online 111.79 GB MBR
5 SAMSUNG HD501LJ Online 465.76 GB GPT
4 WDC WD3200AAKS-00UU3A0 Online 298.09 GB MBR
7 FreeBSD iSCSI Disk SCSI Disk Device Offline 100 GB RAW

I’ll be focusing on disk number 7 here.

In order to have it properly initialized, formatted etc enter the following PS-commands:

Initialize-Disk -Number 7 -PartitionStyle GPT -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume

Initialize-Disk – The command to run.
-Number 7 – Specifies the disk to apply the command to, see output above (result form Get-Disk).
-PartitionStyle GPT – Specifies the style of the partition, GPT is nice and fun.
-PassThru – Sends items from the interactive window down the pipeline as input to other cmdlets.
New-Partition – Creates a new partition on the disk as it was piped to this command.
-AssignDriveLetter – Assigns a drive letter, either automatically or specified by you.
-UseMaximumSize – Use the entire disk’s space.
Format-Volume – Formats the newly created drive.

And there you go, a “Get-Disk” should now specify the drive as online and available:

Example:

7      FreeBSD iSCSI Disk SCSI Disk Device      Online         100 GB GPT

Update: Qraze has utilized parts of this in order to create an automated script to add,initialize and format disks to virtual machines in Hyper-V. You can read the guide here (in Swedish) here.

Cheers!

//Drakfot

Mastodon