Advertisement
Guest User

remove apps

a guest
Oct 22nd, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  # ---------------------------------------------------------------------------
  2. # Initialization
  3. # ---------------------------------------------------------------------------
  4.  
  5. if ($env:SYSTEMDRIVE -eq "X:")
  6. {
  7.   $script:Offline = $true
  8.  
  9.   # Find Windows
  10.   $drives = get-volume | ? {-not [String]::IsNullOrWhiteSpace($_.DriveLetter) } | ? {$_.DriveType -eq 'Fixed'} | ? {$_.DriveLetter -ne 'X'}
  11.   $drives | ? { Test-Path "$($_.DriveLetter):\Windows\System32"} | % { $script:OfflinePath = "$($_.DriveLetter):\" }
  12.   Write-Verbose "Eligible offline drive found: $script:OfflinePath"
  13. }
  14. else
  15. {
  16.   Write-Verbose "Running in the full OS."
  17.   $script:Offline = $false
  18. }
  19.  
  20.  
  21. # ---------------------------------------------------------------------------
  22. # Get-LogDir:  Return the location for logs and output files
  23. # ---------------------------------------------------------------------------
  24.  
  25. function Get-LogDir
  26. {
  27.   try
  28.   {
  29.     $ts = New-Object -ComObject Microsoft.SMS.TSEnvironment -ErrorAction Stop
  30.     if ($ts.Value("LogPath") -ne "")
  31.     {
  32.       $logDir = $ts.Value("LogPath")
  33.     }
  34.     else
  35.     {
  36.       $logDir = $ts.Value("_SMSTSLogPath")
  37.     }
  38.   }
  39.   catch
  40.   {
  41.     $logDir = $env:TEMP
  42.   }
  43.   return $logDir
  44. }
  45.  
  46. # ---------------------------------------------------------------------------
  47. # Get-AppList:  Return the list of apps to be removed
  48. # ---------------------------------------------------------------------------
  49.  
  50. function Get-AppList
  51. {
  52.   begin
  53.   {
  54.     # Look for a config file.
  55.     $configFile = "$PSScriptRoot\RemoveApps.xml"
  56.     if (Test-Path -Path $configFile)
  57.     {
  58.       # Read the list
  59.       Write-Verbose "Reading list of apps from $configFile"
  60.       $list = Get-Content $configFile
  61.     }
  62.     else
  63.     {
  64.       # No list? Build one with all apps.
  65.       Write-Verbose "Building list of provisioned apps"
  66.       $list = @()
  67.       if ($script:Offline)
  68.       {
  69.         Get-AppxProvisionedPackage -Path $script:OfflinePath | % { $list += $_.DisplayName }
  70.       }
  71.       else
  72.       {
  73.         Get-AppxProvisionedPackage -Online | % { $list += $_.DisplayName }
  74.       }
  75.  
  76.       # Write the list to the log path
  77.       $logDir = Get-LogDir
  78.       $configFile = "$logDir\RemoveApps.xml"
  79.       $list | Set-Content $configFile
  80.       Write-Information "Wrote list of apps to $logDir\RemoveApps.xml, edit and place in the same folder as the script to use that list for future script executions"
  81.     }
  82.  
  83.     Write-Information "Apps selected for removal: $list.Count"
  84.   }
  85.  
  86.   process
  87.   {
  88.     $list
  89.   }
  90.  
  91. }
  92.  
  93. # ---------------------------------------------------------------------------
  94. # Remove-App:  Remove the specified app (online or offline)
  95. # ---------------------------------------------------------------------------
  96.  
  97. function Remove-App
  98. {
  99.   [CmdletBinding()]
  100.   param (
  101.         [parameter(Mandatory=$true,ValueFromPipeline=$true)]
  102.         [string] $appName
  103.   )
  104.  
  105.   begin
  106.   {
  107.     # Determine offline or online
  108.     if ($script:Offline)
  109.     {
  110.       $script:Provisioned = Get-AppxProvisionedPackage -Path $script:OfflinePath
  111.     }
  112.     else
  113.     {
  114.       $script:Provisioned = Get-AppxProvisionedPackage -Online
  115.       $script:AppxPackages = Get-AppxPackage
  116.     }
  117.   }
  118.  
  119.   process
  120.   {
  121.     $app = $_
  122.  
  123.     # Remove the provisioned package
  124.     Write-Information "Removing provisioned package $_"
  125.     $current = $script:Provisioned | ? { $_.DisplayName -eq $app }
  126.     if ($current)
  127.     {
  128.       if ($script:Offline)
  129.       {
  130.         $a = Remove-AppxProvisionedPackage -Path $script:OfflinePath -PackageName $current.PackageName
  131.       }
  132.       else
  133.       {
  134.         $a = Remove-AppxProvisionedPackage -Online -PackageName $current.PackageName
  135.       }
  136.     }
  137.     else
  138.     {
  139.       Write-Warning "Unable to find provisioned package $_"
  140.     }
  141.  
  142.     # If online, remove installed apps too
  143.     if (-not $script:Offline)
  144.     {
  145.       Write-Information "Removing installed package $_"
  146.       $current = $script:AppxPackages | ? {$_.Name -eq $app }
  147.       if ($current)
  148.       {
  149.         $current | Remove-AppxPackage
  150.       }
  151.       else
  152.       {
  153.         Write-Warning "Unable to find installed app $_"
  154.       }
  155.     }
  156.  
  157.   }
  158. }
  159.  
  160.  
  161. # ---------------------------------------------------------------------------
  162. # Main logic
  163. # ---------------------------------------------------------------------------
  164.  
  165. $logDir = Get-LogDir
  166. Start-Transcript "$logDir\RemoveApps.log"
  167.  
  168. Get-AppList | Remove-App
  169.  
  170. Stop-Transcript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement