PowerShell: Repeat while an application exist

Darien Chiba 41 Reputation points

I realized that an application is not always removed in the first attempt at PowerShell. So I wanted to create a loop that repeated the application removal command while it still exists. Something like that:

While application exists
Get-AppXPackage <App> | Remove-AppXPackage

How do I do this in PowerShell? And is it possible to do this in a single line?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,347 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 95,181 Reputation points MVP

    Hi @Darien Chiba ,

    The Do...While should help:

    # Simple example  
    $a = 1  
    Do {$a ; $a++} While ($a -lt 5)  

    You can try this if it works for you:

    Do {Get-AppXPackage <App> | Remove-AppXPackage} While (Get-AppXPackage <App>)   


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Andreas Baumgarten

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Darien Chiba 41 Reputation points

    Just a resalva: that exclamation point should be removed. The correct command is:

    Do {Get-AppXPackage <App> | Remove-AppXPackage} While (Get-AppXPackage <App>)