Automation

 View Only
Expand all | Collapse all

Removing recovery partition using PowerShell

  • 1.  Removing recovery partition using PowerShell

    Posted Mar 01, 2022 03:22 PM

    Hi,

    We have 500 MB as recovery partition. We would like to remove that partition from all of our VMs. Is there any powershell script which can do that. 



  • 2.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 03:35 PM

    Thread reported so moderators know it should be moved to the area for PowerCLI.

     



  • 3.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 05:23 PM

    Assuming you are talking about a Windows Guest OS, you could try the Remove-Partition cmdlet.



  • 4.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 05:42 PM

    Yes, you are correct. This is required from all Win 10 VDIs.



  • 5.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 05:44 PM

    This is what I wrote so far but does not work yet


    $File = Get-Filename
    $csv = Import-Csv "$file"

    foreach($item in $csv)
    {
    Get-VM -Name $($item.name) | Stop-VM -confirm:$false

    Remove-Partition -DiskNumber ((Get-Disk).Number) -PartitionNumber ((Get-Partition -DiskNumber ((Get-Disk).Number) | Where-Object {$_.Type -eq 'Recovery'}).PartitionNumber) -PassThru -Confirm:$false | Start-VM
    }



  • 6.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 05:52 PM

    Not sure what the Get-FileName is supposed to do.

    Is the Get-Partition actually returning a Recovery partition?



  • 7.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 06:30 PM

    Sorry, pasted only half of the script. 

    Function Get-FileName($InitialDirectory)
    {
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "CSV (*.csv) | *.csv"
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.FileName
    }



  • 8.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 06:39 PM

    And is Get-Partition returning a Recovery partition?



  • 9.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 07:47 PM

    This gives the recovery partition details. Need to use this in the remove-partition line

     

    Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Recovery'}



  • 10.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 08:05 PM

    And this does not work?

    Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Recovery'} |
    Remove-Partition 

    Note that this has to be executed from an elevated PS prompt



  • 11.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 08:48 PM

    I used these two lines but it did not remove it. It is shutting down the VM but does not remove

     

    Get-VM -Name $($item.name) | Stop-VM -confirm:$false

    Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Recovery'} | Remove-Partition -PassThru -Confirm:$false | Start-VM



  • 12.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 08:55 PM

    I'm confused, you stop the VM, and then want the Guest OS to remove a partition?
    Are you running the Get-Partition and Remove-Partition from an elevated PS prompt inside the Guest OS?



  • 13.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 09:07 PM

    Sorry for not being clear.

    I will have a csv file with VM names. The script will power off the VMs and then go one by one and remove the partition and then power them on. does it make sense?



  • 14.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 09:19 PM

    Did you actually read my last reply?
    You have to run those cmdlets inside the Guest OS running on that VM.
    And that is not possible when you stop the VM



  • 15.  RE: Removing recovery partition using PowerShell

    Posted Mar 01, 2022 09:33 PM

    Oh I missed that. My bad. 

    I will test using SCCM to push this script to run remotely when the VM is running 



  • 16.  RE: Removing recovery partition using PowerShell

    Posted Oct 03, 2022 07:19 PM

    I tested the scripts, it works.

    BTW, there is no Type of "Recovery", it is "Unknown", the "Unknown" is the "Recovery Partition from Windows Disk manager.

    PS C:\WINDOWS\system32> Get-Partition


    DiskPath: \\?\scsi#disk&ven_vmware&prod_virtual_disk#5&35015fb0&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}

    PartitionNumber DriveLetter Offset Size Type
    --------------- ----------- ------ ---- ----
    1 1048576 350 MB IFS
    2 C 368050176 74.22 GB IFS
    3 80056680448 450 MB Unknown

    PS C:\WINDOWS\system32> Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Unknown'}

    DiskPath: \\?\scsi#disk&ven_vmware&prod_virtual_disk#5&35015fb0&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}

    PartitionNumber DriveLetter Offset Size Type
    --------------- ----------- ------ ---- ----
    3 80056680448 450 MB Unknown

    S C:\WINDOWS\system32> Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Unknown'} | Remove-Partition -PassThru -Confirm:$false


    DiskPath: \\?\scsi#disk&ven_vmware&prod_virtual_disk#5&35015fb0&0&000000#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}

    PartitionNumber DriveLetter Offset Size Type
    --------------- ----------- ------ ---- ----
    3 80056680448 450 MB Unknown

    PS C:\WINDOWS\system32> Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Unknown'}

    PS C:\WINDOWS\system32>

     



  • 17.  RE: Removing recovery partition using PowerShell

    Posted Oct 03, 2022 08:06 PM

    wetnose88_0-1664827100553.png

    I did the same thing from my other Windows 2019 server, ONLY within disk manager or disk part, it is called "Recovery", from Get-Partition  it is "Unknown" under the Type column

    You do not need to Power of a VM in order to do it, it can be done online without impact anything

    Disk manager after I deleted the "Unknown" partition using 

    Get-Partition | Where-Object -FilterScript {$_.Type -eq 'Unknown'}| Remove-Partition -PassThru -Confirm:$false | ft -AutoSize

    The same can be done within diskpart as well.

    wetnose88_1-1664827383412.png

     



  • 18.  RE: Removing recovery partition using PowerShell

    Posted Oct 04, 2022 04:34 PM

    The following is the screen log I did on one of the Windows server 2019 using diskpart

    #############################################################################################

    C:\>diskpart

    Microsoft DiskPart version 10.0.17763.1911

    Copyright (C) Microsoft Corporation.
    On computer: egncn6

    DISKPART> list disk              

    Disk ### Status Size Free Dyn Gpt
    -------- ------------- ------- ------- --- ---
    Disk 0 Online 170 GB 20 GB
    Disk 1 Online 100 GB 1024 KB

    DISKPART> select disk

    The arguments specified for this command are not valid.
    For more information on the command type: HELP SELECT DISK

    There is no disk selected.

    DISKPART> select disk 0

    Disk 0 is now the selected disk.

    DISKPART> list disk

    Disk ### Status Size Free Dyn Gpt
    -------- ------------- ------- ------- --- ---
    * Disk 0 Online 170 GB 20 GB
    Disk 1 Online 100 GB 1024 KB

    DISKPART> list partition

    Partition ### Type Size Offset
    ------------- ---------------- ------- -------
    Partition 1 Primary 500 MB 1024 KB
    Partition 2 Primary 149 GB 501 MB
    Partition 3 Recovery 501 MB 149 GB

    DISKPART> select partition 3

    Partition 3 is now the selected partition.

    DISKPART> delete partition override

    DiskPart successfully deleted the selected partition.

    DISKPART> exit

    Leaving DiskPart...



  • 19.  RE: Removing recovery partition using PowerShell

    Posted Jan 05, 2023 07:19 PM

    Please verify the label of the Partition because some time has the "Unknown" label name and the system can not found the name if you are using "Recovery" when you want remove  it



  • 20.  RE: Removing recovery partition using PowerShell

    Posted Jul 19, 2023 12:13 AM

    I was able to remove the Recovery partition with this command. Thanks!

    I had issues to resize my disk as the Recovery was between C: and the unallocated disk, after removing it I was able to expand the disk.