Forum Discussion
leonmoodley
May 16, 2024Copper Contributor
Unable to suppress confirmation popups in Powershell for Format-volume and Remove-Partition
Hi I use the script below to format a secondary drive, I am using the script below ( it basically checks for the required disk size and attempts to format it. If the disk exists already the parti...
- May 20, 2024
For some reason if I run
Stop-Service -Name ShellHWDetection
This seems to have fixed the issue, I start the service again when the rest of the code finishes. Thanks for the assistance. Much appreciated
May 17, 2024
leonmoodley Changed the script a bit, added some error handling, etc. 🙂 Could you try it? Tested it on a Windows 10 VM myself.
$DD = Get-Disk | Where-Object Size -lt "35GB"
if ($null -ne $dd) {
$Part = $DD | Get-Partition
if ($null -ne $Part) {
Write-Warning "Found partition on disk, removing"
try {
$Part | Remove-Partition -Confirm:$false -ErrorAction Stop | Out-Null
}
catch {
Write-Warning "Error removing partition..."
return
}
}
if ($DD.PartitionStyle -ne "GPT" -or $DD.PartitionStyle -ne "GPT" ) {
Write-host "Disk not initialized.
Initializing" -ForegroundColor Green
try {
$DD | Initialize-Disk -PartitionStyle GPT -Confirm:$false | Out-Null
}
catch {
Write-Warning "Error setting disk to GPT"
return
}
}
if ($DD.PartitionStyle -eq "MBR") {
Write-Host "Partition style is MBR
Setting Partition Style to GPT" -ForegroundColor Green
try {
$dd | Set-Disk -PartitionStyle GPT | Out-Null
}
catch {
Write-Warning "Error changing MBR to GPT"
retrn
}
}
try {
$DD | New-Partition -UseMaximumSize -DriveLetter d -ErrorAction Stop | Out-Null
Write-Host "Creating new partition with driveletter D:\" -ForegroundColor Green
}
catch {
Write-Warning "Error creating new partition D:\"
return
}
try {
Format-Volume -DriveLetter d -FileSystem NTFS -NewFileSystemLabel "Backup" -Confirm:$false -Force:$true -ErrorAction Stop | Out-Null
Write-Host "Formatting drive D:\" -ForegroundColor Green
}
catch {
Write-Warning "Error formatting drive D:\"
return
}
try {
New-Item -Type Directory -Path d:\Backup -Force:$true -Confirm:$false -ErrorAction Stop | Out-Null
Write-Host "Created d:\backup folder" -ForegroundColor Green
}
catch {
Write-Warning "Error creating d:\backup folder"
return
}
}
else {
Write-Warning "Error finding disk smaller than 35GB"
return
}- leonmoodleyMay 20, 2024Copper Contributor
Harm_Veenstra Many thanks for your assistance Harm
The code works  fantastically and completes the format etc, It seems that when the new partition is created the Format Prompt pops up. The script does what it is supposed to do, but unfortunately this Pop Up still appears. I'll keep investigating and see if I can figure out whats causing it.
- May 20, 2024I didn't get that message during testing in my VM... Weird... Could you try disabling automount temporarily using this article for testing? https://www.tenforums.com/tutorials/117336-enable-disable-automount-new-disks-drives-windows.html
- leonmoodleyMay 20, 2024Copper Contributor
For some reason if I run
Stop-Service -Name ShellHWDetection
This seems to have fixed the issue, I start the service again when the rest of the code finishes. Thanks for the assistance. Much appreciated