Clear Disk - PowerShell


Overview

Clear-Disk securely erases all data and removes the partitions from a specified disk, leaving it in a pristine and unallocated state. It’s commonly used to prepare disks for new installations, remove sensitive data, or securely dispose of old storage devices.

Syntax

Clear-Disk -DiskNumber <Int> [-Confirm] [-Force] [-InputObject <PSDisk>] [-SkipOEM]

Options/Flags

| Option | Description | Default Value |
|—|—|—|
| -DiskNumber | Specifies the disk number of the target disk to be cleared. | Required |
| -Confirm | Prompts the user for confirmation before executing the operation. | False |
| -Force | Suppresses the confirmation prompt and proceeds with the operation. | False |
| -InputObject | Accepts a PSDisk object as input, allowing scripting and automation. | N/A |
| -SkipOEM | Excludes the OEM partition from the clearing process. | False |

Examples

1. Clear Disk 0 with Confirmation:

Clear-Disk -DiskNumber 0 -Confirm

2. Clear Disk 1 Without Confirmation:

Clear-Disk -DiskNumber 1 -Force

3. Clear a Disk Using a PSDisk Object:

$disk = Get-Disk | Where-Object {$_.DiskNumber -eq 2}
Clear-Disk -InputObject $disk

4. Clear a Disk Excluding the OEM Partition:

Clear-Disk -DiskNumber 3 -SkipOEM

Common Issues

1. Disk Not Found: Ensure the specified disk number exists and is accessible.

2. Drive Is In Use: Close any applications or processes that may be using the target disk.

3. Operation Failed: Check the Event Viewer for any errors related to the Clear-Disk operation.

Integration

1. Prepare Disk for OS Installation:

Clear-Disk -DiskNumber 0
Install-WindowsOS -TargetDisk 0

2. Securely Remove Sensitive Data:

Clear-Disk -DiskNumber 1 -Force
  • Format-Disk – Formats a disk with a specified file system.
  • Initialize-Disk – Initializes a disk with a GUID Partition Table (GPT) or Master Boot Record (MBR).