Forum Discussion
Unable to suppress confirmation popups in Powershell for Format-volume and Remove-Partition
- 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
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
}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- May 20, 2024Ah π nice find!
- leonmoodleyMay 20, 2024Copper Contributor
Hi Harm
I have tested with automount disabled but still the same result. I can confirm though that the command below
$DD | New-Partition -UseMaximumSize -DriveLetter d -ErrorAction Stop | Out-Nullis the one which triggers the POP up message for some weird reason.
This also seems to be the case when the script runs again and finds a partition, The Remove partion command also triggers the pop up.
Once again many thanks for your help