#Function to remove
directory using Robocopy /PURGE by taking the ownership of the directory to be
removed
Function
Remove_Dir ($DirToRemove)
{
$TempDir
= "C:\Windows\Temp\Test"
Write-Output "Removing
Dir $DirToRemove"
If(Test-path
$TempDir){
}
Else{ $TempDir
= New-Item "C:\Windows\Temp\Test" -type directory
}
takeown /R
/F $DirToRemove
/D N
icacls $DirToRemove
/T /grant
administrators:F /GRANT
SYSTEM:F
robocopy
"$TempDir" "$DirToRemove"
/PURGE
$EmptyFolder = Get-ChildItem $DirToRemove -Recurse
If ($EmptyFolder
-eq $Null)
{ Remove-Item -Path
$DirToRemove}
}
$ModulePath
= "C:\Program
Files\Microsoft Application
Virtualization\Client\AppvClient\AppvClient.psd1"
$PackageInstallationRoot
= "C:\ProgramData\App-V"
$RemoveAllPackages
= $true
$RemoveAllRegs
= $true
$all
= ""
if
($RemoveAllPackages)
{
if (!$PackageInstallationRoot)
{
Write-Error "PackageInstallationRoot
is required for removing packages"
exit 3
}
$packageFiles = Get-ChildItem ([System.Environment]::ExpandEnvironmentVariables($PackageInstallationRoot))
if (!$packageFiles -or
$packageFiles.Count
-eq 0)
{
Write-Output "No
package files found, nothing to clean up. Exiting."
exit 0
}
}
$clientService
= get-service
appvclient
if
(!$clientService)
{
Write-Output "The
client service was not found. Install cannot clean up package files."
exit 5
}
else
{
if ($clientService.Status -ne "Running")
{
Write-Output "The
client service is not running. Install
cannot clean up package files."
exit 4
}
}
Write-Output
"Shutting down all applications and the App-V
Client Service."
if
($RemoveAllPackages)
{
Write-Output "Uninstall
detected: All App-V Packages will be removed."
}
# load the client
import-module
$ModulePath
Set-AppVClientMode
-Uninstall
# shutdown all active
Connection Groups
Write-Output
"Stopping all connection groups."
Get-AppvClientConnectionGroup
-All | Stop-AppvClientConnectionGroup -Global
# poll while there are
still active connection groups
$connectionGroups
= Get-AppvClientConnectionGroup
-All
$connectionGroupsInUse
= $FALSE
do
{
$connectionGroupsInUse = $FALSE
ForEach ($connectionGroup
in $connectionGroups)
{
if ($connectionGroup.InUse -eq $TRUE)
{
$connectionGroupsInUse
= $TRUE
Write-Host
"Stopping connection group " $connectionGroup.Name
Stop-AppvClientConnectionGroup
$connectionGroup -Global
#
allow 1 second for the VE to tear down before we continue polling
sleep
1;
}
}
} while
($connectionGroupsInUse)
# shutdown all active
Packages
Write-Output
"Stopping all packages."
Get-AppvClientPackage
-All | Stop-AppvClientPackage -Global
# poll while there are
still active packages
$packages
= Get-AppvClientPackage
-All
$packagesInUse
= $FALSE
do
{
$packagesInUse = $FALSE
ForEach ($package
in $packages)
{
if ($package.InUse -eq $TRUE)
{
$packagesInUse
= $TRUE
Write-Host
"Stopping package " $package.Name
Stop-AppvClientPackage
$package -Global
#
allow 1 second for the VE to tear down before we continue polling
sleep
1
}
}
} while
($packagesInUse)
if
($RemoveAllPackages)
{
$connectionGroups = Get-AppvClientConnectionGroup
-All
Write-Output "Removing
all App-V Connection Groups"
ForEach ($connectionGroup
in $connectionGroups)
{
Remove-AppvClientConnectionGroup $connectionGroup
}
$packages =
Get-AppvClientPackage -All
Write-Output "Removing
all App-V Packages"
ForEach ($package
in $packages)
{
Remove-AppvClientPackage $package
}
}
Write-Output
"Removing all remaining package folders"
if
($RemoveAllPackages -and $PackageInstallationRoot)
{
# Delete the App-V package directories in $PackageInstallationRoot
$AppVPackages = Get-ChildItem $PackageInstallationRoot
Foreach ($AppVPackage
in $AppVPackages){
Remove_Dir
($AppVPackage.FullName)
}
$ProgramDataDir = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::CommonApplicationData)
$ProgDataAppvClientPath = $ProgramDataDir
+ "\Microsoft\AppV\Client"
#Delete the App-V directories in
$ProgDataAppvClientPath + "\Catalog\PackageGroups"
$ProgDataCatalogCGPath = $ProgDataAppvClientPath
+ "\Catalog\PackageGroups"
$ProgDataCatalogCGs = Get-ChildItem $ProgDataCatalogCGPath
Foreach ($ProgDataCatalogCG
in $ProgDataCatalogCGs){
Remove_Dir
($ProgDataCatalogCG.FullName)
}
#Delete the App-V directories in
$ProgDataAppvClientPath + "\Catalog\Packages"
$ProgDataCatalogPKGPath = $ProgDataAppvClientPath
+ "\Catalog\Packages"
$ProgDataCatalogPKGs = Get-ChildItem $ProgDataCatalogPKGPath
Foreach ($ProgDataCatalogPKG
in $ProgDataCatalogPKGs){
Remove_Dir
($ProgDataCatalogPKG.FullName)
}
#Delete the App-V files in
$ProgDataAppvClientPath + "\VREG"
$ProgDataVREGPath = $ProgDataAppvClientPath
+ "\VREG"
$ProgDataVREGs = Get-ChildItem $ProgDataVREGPath
Foreach ($ProgDataVREG
in $ProgDataVREGs){
Remove-Item
$ProgDataVREG.FullName
}
#
}
Write-Output
"Removing all HKLM Regestries"
If
($RemoveAllRegs){
#Delete the App-V regestries in HKLM:\SOFTWARE\Microsoft\AppV\Client\Packages
$AppVReg_HKLMPackages = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\AppV\Client\Packages"
foreach ($AppVReg_HKLMPackage
in $AppVReg_HKLMPackages
) {
Set-Location "HKLM:\SOFTWARE\Microsoft\AppV\Client\Packages"
Remove-Item -Path
$AppVReg_HKLMPackage.PSChildName -Recurse
-Force
}
#Delete the App-V regestries in
HKLM:\SOFTWARE\Microsoft\AppV\Client\PackageGroups
$AppVReg_HKLMPackageGroups = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\AppV\Client\PackageGroups"
foreach ($AppVReg_HKLMPackageGroup
in $AppVReg_HKLMPackageGroups
) {
Set-Location "HKLM:\SOFTWARE\Microsoft\AppV\Client\PackageGroups"
Remove-Item -Path
$AppVReg_HKLMPackageGroup.PSChildName -Recurse
-Force
}
#Delete the App-V regestries in
HKLM:\SOFTWARE\Microsoft\AppV\Client\Streaming\Packages
$AppVReg_HKLMStreams = Get-ChildItem "HKLM:\SOFTWARE\Microsoft\AppV\Client\Streaming\Packages"
foreach ($AppVReg_HKLMStream
in $AppVReg_HKLMStreams
) {
Set-Location "HKLM:\SOFTWARE\Microsoft\AppV\Client\Streaming\Packages"
Remove-Item -Path
$AppVReg_HKLMStream.PSChildName -Recurse -Force
}
#Delete the App-V regestries in
HKLM:\Software\Microsoft\AppV\Client\Integration\Packagess
$AppVReg_HKLMIntegrationPkgs = Get-ChildItem "HKLM:\Software\Microsoft\AppV\Client\Integration\Packages"
foreach ($AppVReg_HKLMIntegrationPkg
in $AppVReg_HKLMIntegrationPkgs
) {
Set-Location "HKLM:\Software\Microsoft\AppV\Client\Integration\Packages"
Remove-Item -Path
$AppVReg_HKLMIntegrationPkg.PSChildName -Recurse
-Force
}
}
Set-AppVClientMode
-Normal