Forum Discussion

leonmoodley's avatar
leonmoodley
Copper Contributor
May 16, 2024
Solved

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 partition is deleted and recreated ( sort of a reset function if my device is re configured )

 

Despite using the -Confirm:$false parameters I am still getting pop messages asking for confirmation. I am using Windows 10 IOT LTSC 2021. The process for setting the drive needs to be automated so confirmation boxes won't work for me. Any advice will be greatly appreciated.

 

DD = get-disk |Where {$_.size -lt "35GB"}

$Part = $DD |Get-Partition
$Part |Remove-Partition -Confirm:$false
if ($DD.PartitionStyle -ne "GPT" -or $DD.PartitionStyle -ne "GPT" )
{Write-host "Disk not initialized.
Initializing" -for Yellow
$DD |Initialize-Disk -PartitionStyle GPT
}


if ($DD.PartitionStyle -eq "MBR")
{
Write-Host "Partition style is MBR
Setting Partition Style to GPT" -Fore Yellow
$dd |Set-Disk -PartitionStyle GPT
}


$DD | New-Partition -UseMaximumSize -DriveLetter d

Format-Volume -DriveLetter d -FileSystem NTFS -NewFileSystemLabel "Backup" -Confirm:$false

MD d:\Backup

  • leonmoodley's avatar
    leonmoodley
    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

6 Replies

  • 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
    }

Resources