Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a disk using PowerShell without prompting for confirmation

I have added data disks to Azure virtual machines and I need to create a volume for them.

I have used the following code to create the volume:

$disk = Get-Disk | where-object PartitionStyle -eq "RAW"  
$disk | Initialize-Disk -PartitionStyle GPT  
$partition = $disk | New-Partition -UseMaximumSize -DriveLetter F  
$partition | Format-Volume -Confirm:$false -Force  

While creating the volume, it asks for confirmation before formatting the disk.

I want to avoid this confirmation box. I tried -Confirm:$false -Force but it still prompts for confirmation.

like image 788
s p Avatar asked Mar 06 '17 08:03

s p


People also ask

How do I wipe a disk in PowerShell?

To format the drive type in Clear-Disk -Number X -RemoveData replacing X with your drive number. Click Y and press Enter to erase the drive. You will see a splash screen and return to an empty line in Powershell when completed.

What does the PowerShell command initialize disk do?

The Initialize-Disk cmdlet initializes a Disk object with the RAW partition style to either the MBR or GPT partition styles. The default partition style is GPT. Disks must be initialized before they can be formatted and used to store data.


2 Answers

This fixed my problem. Confirmation popup does not appears while using the following code.

$disk = Get-Disk | where-object PartitionStyle -eq "RAW"  
Initialize-Disk -Number $disk.Number -PartitionStyle MBR -confirm:$false  
New-Partition -DiskNumber $disk.Number -UseMaximumSize -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel "Local Disk" -confirm:$False  
Set-Partition -DiskNumber $disk.Number -PartitionNumber 1 -NewDriveLetter F  
like image 111
s p Avatar answered Nov 15 '22 07:11

s p


It appears this may be a bug with how Format-Volume handles -Confirm as discussed here: https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11088429-format-volume-force-parameter-does-not-work

The workaround suggested is to set $confirmpreference = 'none' before you start.

You might also want to grab the current $confirmpreference in to a variable first and then put it back to what it was afterwards. E.g:

$disk = Get-Disk | where-object PartitionStyle -eq "RAW"  
$disk | Initialize-Disk -PartitionStyle GPT  
$partition = $disk | New-Partition -UseMaximumSize -DriveLetter F  

$currentconfirm = $confirmpreference
$confirmpreference = 'none'
$partition | Format-Volume -Force  
$confirmpreference = $currentconfirm 
like image 39
Mark Wragg Avatar answered Nov 15 '22 06:11

Mark Wragg