locked
Get-AppXPackage returns result after script quits RRS feed

  • Question

  • I have a problem with this script:

    Write-Host "`nList of current Universal Windows Apps:"
    Write-Host "-----"
    (Get-AppXPackage -AllUsers).Name | Sort-Object -Unique
    
    Write-Host "`n`nList of Dependencies:"
    Write-Host "-----"
    (Get-AppXPackage -AllUsers).Dependencies | Select-Object -Property Name -Unique | Sort-Object -Property Name
    
    Read-Host -Prompt "`n`nPress Enter to continue"
    Exit

    I kindly ask you to save this script as "AnyName.ps1" and run it (right click on it and select "Run with PowerShell").

    What you should notice is that "dependencies" are listed but after "Enter is pressed" and at that point is already too late because the script is exiting. Why is this happening?

    I have tried entering another "Write-Host" line with the separator ("-----") after the second "Get-AppXPackage" command. Nothing.

    I have tried sleeping the script for 5 seconds after the second "Get-AppXPackage" and still nothing.

    I do not understand what am I doing wrong. Thank you for all your help.

    I have Windows 10 v1709 x64 with PowerShell v5.1 build 16299.

    Monday, March 12, 2018 7:01 PM

Answers

  • You miss the point.  You cannot mix direct console IO with those commands.  The commands run async and the console read blocks all output until it finishes.  That is just the behavior when mixing pipeline output and console reads.


    \_(ツ)_/

    • Marked as answer by khrbud Monday, March 12, 2018 7:53 PM
    Monday, March 12, 2018 7:51 PM

All replies

  • This is an issue with using mixed output and Write-Host and Read-Host.

    Don't use "Read-Host" or "exit" to end a script.


    \_(ツ)_/

    Monday, March 12, 2018 7:16 PM
  • I am sorry but the same thing happens if I write the script like this:
    Write-Host "`nList of current Universal Windows Apps:"
    Write-Host "-----"
    (Get-AppXPackage -AllUsers).Name | Sort-Object -Unique
    
    Write-Host "`n`nList of Dependencies:"
    Write-Host "-----"
    (Get-AppXPackage -AllUsers).Dependencies | Select-Object -Property Name -Unique | Sort-Object -Property Name
    
    [Void][System.Console]::ReadKey($True)
    How should I pause the script then if I want to execute the script with "Run with PowerShell" right click option?

    Monday, March 12, 2018 7:29 PM
  • You miss the point.  You cannot mix direct console IO with those commands.  The commands run async and the console read blocks all output until it finishes.  That is just the behavior when mixing pipeline output and console reads.


    \_(ツ)_/

    • Marked as answer by khrbud Monday, March 12, 2018 7:53 PM
    Monday, March 12, 2018 7:51 PM
  • Work-around -  use a job and wait on the job.


    \_(ツ)_/

    Monday, March 12, 2018 7:56 PM
  • OK, thanks. I have never worked with Start-Job, Wait-Job and Receive-Job but I am willing to try. If I am successful I will post my code here. Thank you very much.
    Monday, March 12, 2018 8:12 PM
  • Here goes nothing:

    Write-Host "`nList of current Universal Windows Apps:"
    Write-Host "-----"
    $jList = Start-Job -ScriptBlock { (Get-AppXPackage -AllUsers).Name | Sort-Object -Unique }
    Wait-Job -Job $jList -Timeout 10
    Receive-Job -Job $jList -Wait -AutoRemoveJob
    
    Write-Host "`n`nList of Dependencies:"
    Write-Host "-----"
    $jList = Start-Job -ScriptBlock { (Get-AppXPackage -AllUsers).Dependencies | Select-Object -Property Name -Unique | Sort-Object -Property Name }
    Wait-Job -Job $jList -Timeout 10
    Receive-Job -Job $jList -Wait -AutoRemoveJob | Select-Object -Property Name
    
    Read-Host -Prompt "`n`nPress Enter to continue"
    Exit

    Once again, thank you "jrv"

    Monday, March 12, 2018 8:54 PM
  • $sb = {
        (Get-AppXPackage -AllUsers).Dependencies | Sort-Object -Property Name 
    }
    Start-Job -ScriptBlock $sb |
        Receive-Job -Wait -AutoRemoveJob | 
        Select-Object -Property Name


    \_(ツ)_/

    Monday, March 12, 2018 9:04 PM
  • Couldn't get your one-liner to work. I don't know why.

    So, this is the whole script ... Removing UWA based on the Whit-list

    # Remove Windows Apps
    #
    # _________________________
    # PowerShell v4.0
    # Run as Administrator
    #
    # Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
    # PowerShell.exe -Version 4.0 -NoLogo -NonInteractive -WindowStyle Hidden -ExecutionPolicy Unrestricted -File "RemoveWinApps.ps1"
    # _________________________
    #
    # Author: Krešimir Hrbud
    # Date: 12.03.2018.
    
    # Requirements
    # _________________________
    If ((Get-Host).Version.Major -lt 4){
     $sEvent = "You are running PowerShell Host Version " + (Get-Host).Version.Major + ". This version is not supported.`n"
     $sEvent += "Please use PowerShell Host Version 4 (or higher) and run this script again.`n"
     Write-Warning $sEvent
     Exit }
    
    $OS = (Get-CimInstance Win32_OperatingSystem).Caption
    If (!$OS.contains("Windows 10")){
     $sEvent = "You are running `"$OS`". This version of Windows is not supported.`n"
     $sEvent += "Please use `"Windows 10`" and run this script again.`n"
     Write-Warning $sEvent
     Exit }
    
    If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] “Administrator”)){
     $sEvent = "You do not have Administrative rights! Would you like to execute this script as an Administrator?"
     Write-Warning $sEvent
     $sTitle; $sMessage
     $vYes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Tries to run this script as an Administrator."
     $vNo = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Exits."
     $oOptions = [System.Management.Automation.Host.ChoiceDescription[]]($vYes, $vNo)
     $iAnswer = $Host.UI.PromptForChoice($sTitle, $sMessage, $oOptions, 0) # "0" for "first item ($vYes) is default choice"
     Switch ($iAnswer){
      0 { Start-Process -FilePath "$PSHome\PowerShell.exe" -ArgumentList "-NoLogo -NoProfile -ExecutionPolicy Unrestricted -File `"$PSCommandPath`"" -Verb RunAs }
      1 { $Null } }
     Exit }
    # _________________________
    
    
    $sSeparator = "-" * 75
    
    $sTitle    = "`n$sSeparator`nRemove Windows Apps`n$sSeparator`n"
    $sMessage  = "Script will try to uninstall Universal Windows Apps according to the specified white-list.`n"
    $sMessage += "Do you wish to continue?`n"
    $vYes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Uninstalls Universal Windows Apps."
    $vNo = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Displays Universal Windows Apps, their Dependencies and Exits."
    $oOptions = [System.Management.Automation.Host.ChoiceDescription[]]($vYes, $vNo)
    $iAnswer = $Host.UI.PromptForChoice($sTitle, $sMessage, $oOptions, 1) # "0" for "first item ($vYes) is default choice"
    Switch ($iAnswer){
     0 { $Null }
     1 { [System.Console]::ForegroundColor = "Yellow"
         Write-Host "`nList of Universal Windows Apps:"
         Write-Host $sSeparator
         (Get-AppXPackage -AllUsers).Name | Sort-Object -Unique
         Write-Host "$sSeparator"
    
         [System.Console]::ForegroundColor = "Cyan"
         Write-Host "`nList of Dependencies:"
         Write-Host $sSeparator
         $jList = Start-Job -ScriptBlock { (Get-AppXPackage -AllUsers).Dependencies | Select-Object -Property Name -Unique | Sort-Object -Property Name }
         Wait-Job -Job $jList -Timeout 10
         Receive-Job -Job $jList -Wait -AutoRemoveJob | Select-Object -Property Name
         Write-Host "$sSeparator"
    
         [System.Console]::ResetColor()
         Read-Host -Prompt "`n`nPress Enter to continue"
         Exit } }
    
    
    # ____________________________________________________________________________________________________
    #
    # White-list of apps that should not be removed
    # ____________________________________________________________________________________________________
    #
    # List of Universal Windows 10 Apps ... https://docs.microsoft.com/en-us/windows/application-management/apps-in-windows-10
    #
    
    $aWhiteList = # SYSTEM APPS ... (Get-AppXPackage -AllUsers).Name | Sort-Object -Unique
                  # _________________________
    
                  "InputApp",
                  "Microsoft.AAD.BrokerPlugin",
                  "Microsoft.AccountsControl",
                  "Microsoft.BioEnrollment",
                  "Microsoft.CredDialogHost",
                  "Microsoft.ECApp",
                  "Microsoft.LockApp",
                  "Microsoft.PPIProjection",
                  "Microsoft.Windows.Apprep.ChxApp",
                  "Microsoft.Windows.AssignedAccessLockApp",
                  "Microsoft.Windows.CloudExperienceHost",
                  "Microsoft.Windows.ContentDeliveryManager",
                  "Microsoft.Windows.HolographicFirstRun",
                  "Microsoft.Windows.OOBENetworkCaptivePortal",
                  "Microsoft.Windows.OOBENetworkConnectionFlow",
                  "Microsoft.Windows.ParentalControls",
                  "Microsoft.Windows.PeopleExperienceHost",
                  "Microsoft.Windows.PinningConfirmationDialog",
                  "Microsoft.Windows.SecHealthUI",
                  "Microsoft.Windows.SecondaryTileExperience",
                  "Microsoft.Windows.SecureAssessmentBrowser",
                  "Microsoft.XboxGameCallableUI",
    
                  # PROVISIONED APPS
                  # _________________________
    
                  #"Microsoft.BingNews",
                  "Microsoft.BingWeather",
                  #"Microsoft.GetHelp",
                  #"Microsoft.Messaging",
                  #"Microsoft.MicrosoftSolitaireCollection",
                  "Microsoft.MicrosoftStickyNotes",
                  "Microsoft.MSPaint",
                  #"Microsoft.People",
                  #"Microsoft.Print3D",
                  #"Microsoft.RemoteDesktop",
                  #"Microsoft.Windows.Photos",
                  #"Microsoft.WindowsAlarms",
                  "Microsoft.WindowsCalculator",
                  #"Microsoft.WindowsCamera",
                  #"Microsoft.WindowsCommunicationsApps",
                  #"Microsoft.WindowsMaps",
                  "Microsoft.WindowsSoundRecorder",
                  #"Microsoft.ZuneMusic",
                  #"Microsoft.ZuneVideo",
    
                  # DEPENDENCIES ... (Get-AppXPackage -AllUsers).Dependencies | Select-Object -Property Name -Unique | Sort-Object -Property Name
                  # _________________________
    
                  "Microsoft.Advertising.Xaml",
                  "Microsoft.NET.Native.Framework",
                  "Microsoft.NET.Native.Runtime",
                  "Microsoft.Services.Store.Engagement",
                  "Microsoft.VCLibs",
    
                  # CRITICAL APPS
                  # _________________________
    
                  "Microsoft.DesktopAppInstaller",
                  "Microsoft.MicrosoftEdge",
                  "Microsoft.StorePurchaseApp",
                  "Microsoft.Windows.Cortana",
                  "Microsoft.Windows.ShellExperienceHost",
                  "Microsoft.WindowsStore",
                  "Windows.ImmersiveControlPanel",
                  "Windows.PrintDialog"
    
    # ____________________________________________________________________________________________________
    
    
    # Removing packages for current user
    [System.Console]::ForegroundColor = "Yellow"
    $oAppsList = (Get-AppXPackage).PackageFullName
    ForEach ($vApp in $oAppsList){
     For ($i = 0; $i -le $aWhiteList.GetUpperBound(0); $i++){
      If ($vApp -like ("*"+$aWhiteList[$i]+"*")){ Break }
      If ($i -eq $aWhiteList.GetUpperBound(0)){
       Write-Host "Removing package: $vApp"
       Remove-AppXPackage -Package $vApp
       Write-Host $sSeparator } } }
    
    
    #Removing provisioned packages
    [System.Console]::ForegroundColor = "Cyan"
    $oAppsList = (Get-AppXProvisionedPackage -Online).PackageName
    ForEach ($vApp in $oAppsList){
     For ($i = 0; $i -le $aWhiteList.GetUpperBound(0); $i++){
      If ($vApp -like ("*"+$aWhiteList[$i]+"*")){ Break }
      If ($i -eq $aWhiteList.GetUpperBound(0)){
       Write-Host "Removing provisioned package: $vApp"
       Remove-AppXProvisionedPackage -Online -PackageName $vApp
       Write-Host $sSeparator } } }
    
    
    # Removing packages for all users
    [System.Console]::ForegroundColor = "Green"
    $oAppsList = (Get-AppXPackage -AllUsers).PackageFullName
    ForEach ($vApp in $oAppsList){
     For ($i = 0; $i -le $aWhiteList.GetUpperBound(0); $i++){
      If ($vApp -like ("*"+$aWhiteList[$i]+"*")){ Break }
      If ($i -eq $aWhiteList.GetUpperBound(0)){
       Write-Host "Removing package: $vApp"
       Remove-AppXPackage -AllUsers -Package $vApp
       Write-Host $sSeparator } } }
    
    
    [System.Console]::ResetColor()
    Read-Host -Prompt "`n`nPress Enter to continue"
    Exit
    

    Monday, March 12, 2018 9:29 PM
  • And yes, I know it's crap but it works for me.
    Monday, March 12, 2018 9:33 PM