- Create a custom role with Azure role-based access control (Azure RBAC) using PowerShell - Wed, Jan 20 2021
- Step by step Deploying Docker Container to Azure using Azure CLI - Wed, Sep 2 2020
- Install Docker offline on Windows Server 2016 - Thu, Dec 6 2018
List disks ^
Before you can begin configuring and managing your Windows Server storage, you first need to fetch the list of disks presented on the machine using PowerShell.
# List all disks Get-Disk # List disks that are not system disks # to avoid accidently formatting your system drive Get-Disk | Where-Object IsSystem -eq $False # List disks that are offline Get-Disk | Where-Object IsOffline –Eq $True
Format a disk ^
Having new disks present on the server is a totally different case, but there may be scenarios where you want to format existing disks and then create partitions and volumes. You can use the Clear-Disk cmdlet to remove all partition information and uninitialize it, which will erase the data on the disk.
This is an unrecoverable process, so please make sure to back up your disk before running the command.
# Clear a blank disk with the disk number Clear-Disk -Number 1
Disks can contain both data and OEM partitions, and you can very easily remove them from the disk using the switch parameters -RemoveData and -RemoveOEM.
# Clear a target disk with data partitions Clear-Disk -Number 1 –RemoveData # Clear a disk regardless of whether it contains data or OEM partitions Clear-Disk -Number 1 -RemoveData –RemoveOEM
Initialize a disk ^
A new disk or a cleaned disk using the cmdlet mentioned above is in an uninitialized state, and you have to initialize it first using the disk number of the target disk.
Initialize-Disk -Number 2
By default, this initializes all disks as GUID Partition Tables (GPTs) unless explicitly specified, and you can use the ‑PartitionStyle parameter to initialize a disk as a Master Boot Record (MBR).
Initialize-Disk 4 –PartitionStyle MBR
If the disk has recently just become present on the server, it will be in a raw partition style state. You can easily target and initialize such disks using the following command:
Get-Disk | Where-Object PartitionStyle –Eq 'RAW' | Initialize-Disk
Create a partition and volume ^
To create a partition, which is basically a block of data on the disk, you can run the New-Partition cmdlet with the ‑AssignDriveLetter and ‑UseMaximumSize switches to choose the available and allowed drive letter with the maximum size available on the disk.
New-Partition –DiskNumber 3 -AssignDriveLetter –UseMaximumSize
You can also explicitly specify the disk size and drive letter.
New-Partition –DiskNumber 4 -Size 150gb -DriveLetter h
After partitioning the disks, you can list them to see the current status:
# Listing partitions on specific disks Get-Partition –DiskNumber 0,2,3
After creating the partitions, they are still not accessible from the File Explorer. You have to create new volumes in a format (like NTFS) that the operating system understands.
# List all volumes Get-Volume # Format volumes with NTFS Format-Volume -DriveLetter F,H -FileSystem NTFS -Confirm:$false
Delete a partition ^
To delete any partition on a disk, you can target it with the disk number and then pipe it to the Remove-Partition cmdlet
# Delete a partition Get-Partition –DiskNumber 2,3 | Remove-Partition -Confirm:$false
After presenting a storage disk on a Windows machine, it is very easy to initialize raw disks and bring them online using PowerShell. With just a few PowerShell cmdlets, you can also create partitions and volumes so they're accessible through the file system.
Subscribe to 4sysops newsletter!
Get-Disk | Where-Object PartitionStyle -eq 'RAW' | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -Confirm:$false