PS: ClickOnce App Uninstall-Reinstall

1 minute read

Description:

Use the following PS Script to uninstall and the reinstall a Click Once application.

To Resolve:

  1. Copy and paste the following in notepad and then save as *.ps1.
Begin
{

Function Open-Internetexplorer
{
Param
(
	[Parameter(Mandatory = $True)]
	[String] $Url,
	[Switch] $Inforeground,
	[Switch] $Fullscreen
)
If ($Inforeground)
{
	Add-Nativehelpertype
}

$Internetexplorer = New-Object -Com "Internetexplorer.Application"
$Internetexplorer.Navigate($Url)
$Internetexplorer.Visible = $True
$Internetexplorer.Fullscreen = $Fullscreen
If ($Inforeground)
{
	[Nativehelper]::Setforeground($Internetexplorer.Hwnd)
}
Return $Internetexplorer
}

Function Add-Nativehelpertype
{
$Nativehelpertypedefinition = 
@"
Using System;
Using System.Runtime.Interopservices;

Public Static Class Nativehelper
{
[Dllimport("User32.Dll")]
[Return: Marshalas(Unmanagedtype.Bool)]
Private Static Extern Bool Setforegroundwindow(Intptr Hwnd);

Public Static Bool Setforeground(Intptr Windowhandle)
{
Return Nativehelper.Setforegroundwindow(Windowhandle);
}

}
"@
If (-Not ([System.Management.Automation.Pstypename] "Nativehelper").Type)
{
	Add-Type -Typedefinition $Nativehelpertypedefinition
}
}

}

Process
{   


#Log "Uninstalling App..." 
$Installedapplicationnotmsi = Get-Childitem Hkcu:\Software\Microsoft\Windows\Currentversion\Uninstall | 
Foreach-Object {Get-Itemproperty $_.Pspath}
$Uninstallstring = $Installedapplicationnotmsi | 
Where-Object { $_.Displayname -Match "App" } | 
Select-Object Uninstallstring 
$Wshell = New-Object -Com Wscript.Shell
$Selecteduninstallstring = $Uninstallstring.Uninstallstring
$Wshell.Run("Cmd /C $Selecteduninstallstring")
Start-Sleep -Seconds 5
$Wshell.Sendkeys("Ok")
$Wshell.Sendkeys("{Enter}")
#Log "Uninstalling App... Completed" -Color Darkred 

#Log "Renaming Localappdata-Apps-2.0 Folder..." 
Set-Location "C:\Users\$Env:Username\Appdata\Local\Apps"
Rename-Item -Path .\2.0 -Newname 2.0.Old -Force
If (Test-Path "C:\Users\$Env:Username\Appdata\Local\Apps\2.0.Old") 
{

#Log "Renaming Localappdata-Apps-2.0 Folder...Completed" 
}
Else 
{ 

#Log "Renaming Localappdata-Apps-2.0 Folder...Failed!" -Color Darkred 
}

#Log "Reinstalling Application..." 

Open-Internetexplorer -Url Http://Example.Com/Install.Application -Fullscreen -Inforeground

$Wshell = New-Object -Com Wscript.Shell
Start-Sleep -Seconds 5
$Wshell.Sendkeys("{Left}")
$Wshell.Sendkeys("{Enter}")	

}

This can be found in my gwApplications section.