PowerShell: Repeat while an application exist

Darien Chiba 41 Reputation points
2022-02-12T02:45:22.793+00:00

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
Wend

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
    2022-02-12T10:08:51.577+00:00

    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)

    Regards
    Andreas Baumgarten

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Darien Chiba 41 Reputation points
    2022-02-14T17:58:44.177+00:00

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

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