Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ---------------------------------------------------------------------------
- # Initialization
- # ---------------------------------------------------------------------------
- if ($env:SYSTEMDRIVE -eq "X:")
- {
- $script:Offline = $true
- # Find Windows
- $drives = get-volume | ? {-not [String]::IsNullOrWhiteSpace($_.DriveLetter) } | ? {$_.DriveType -eq 'Fixed'} | ? {$_.DriveLetter -ne 'X'}
- $drives | ? { Test-Path "$($_.DriveLetter):\Windows\System32"} | % { $script:OfflinePath = "$($_.DriveLetter):\" }
- Write-Verbose "Eligible offline drive found: $script:OfflinePath"
- }
- else
- {
- Write-Verbose "Running in the full OS."
- $script:Offline = $false
- }
- # ---------------------------------------------------------------------------
- # Get-LogDir: Return the location for logs and output files
- # ---------------------------------------------------------------------------
- function Get-LogDir
- {
- try
- {
- $ts = New-Object -ComObject Microsoft.SMS.TSEnvironment -ErrorAction Stop
- if ($ts.Value("LogPath") -ne "")
- {
- $logDir = $ts.Value("LogPath")
- }
- else
- {
- $logDir = $ts.Value("_SMSTSLogPath")
- }
- }
- catch
- {
- $logDir = $env:TEMP
- }
- return $logDir
- }
- # ---------------------------------------------------------------------------
- # Get-AppList: Return the list of apps to be removed
- # ---------------------------------------------------------------------------
- function Get-AppList
- {
- begin
- {
- # Look for a config file.
- $configFile = "$PSScriptRoot\RemoveApps.xml"
- if (Test-Path -Path $configFile)
- {
- # Read the list
- Write-Verbose "Reading list of apps from $configFile"
- $list = Get-Content $configFile
- }
- else
- {
- # No list? Build one with all apps.
- Write-Verbose "Building list of provisioned apps"
- $list = @()
- if ($script:Offline)
- {
- Get-AppxProvisionedPackage -Path $script:OfflinePath | % { $list += $_.DisplayName }
- }
- else
- {
- Get-AppxProvisionedPackage -Online | % { $list += $_.DisplayName }
- }
- # Write the list to the log path
- $logDir = Get-LogDir
- $configFile = "$logDir\RemoveApps.xml"
- $list | Set-Content $configFile
- 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"
- }
- Write-Information "Apps selected for removal: $list.Count"
- }
- process
- {
- $list
- }
- }
- # ---------------------------------------------------------------------------
- # Remove-App: Remove the specified app (online or offline)
- # ---------------------------------------------------------------------------
- function Remove-App
- {
- [CmdletBinding()]
- param (
- [parameter(Mandatory=$true,ValueFromPipeline=$true)]
- [string] $appName
- )
- begin
- {
- # Determine offline or online
- if ($script:Offline)
- {
- $script:Provisioned = Get-AppxProvisionedPackage -Path $script:OfflinePath
- }
- else
- {
- $script:Provisioned = Get-AppxProvisionedPackage -Online
- $script:AppxPackages = Get-AppxPackage
- }
- }
- process
- {
- $app = $_
- # Remove the provisioned package
- Write-Information "Removing provisioned package $_"
- $current = $script:Provisioned | ? { $_.DisplayName -eq $app }
- if ($current)
- {
- if ($script:Offline)
- {
- $a = Remove-AppxProvisionedPackage -Path $script:OfflinePath -PackageName $current.PackageName
- }
- else
- {
- $a = Remove-AppxProvisionedPackage -Online -PackageName $current.PackageName
- }
- }
- else
- {
- Write-Warning "Unable to find provisioned package $_"
- }
- # If online, remove installed apps too
- if (-not $script:Offline)
- {
- Write-Information "Removing installed package $_"
- $current = $script:AppxPackages | ? {$_.Name -eq $app }
- if ($current)
- {
- $current | Remove-AppxPackage
- }
- else
- {
- Write-Warning "Unable to find installed app $_"
- }
- }
- }
- }
- # ---------------------------------------------------------------------------
- # Main logic
- # ---------------------------------------------------------------------------
- $logDir = Get-LogDir
- Start-Transcript "$logDir\RemoveApps.log"
- Get-AppList | Remove-App
- Stop-Transcript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement