Working Powershell Script to do a slient install
[CmdletBinding()]
param (
[Parameter()]
[switch]$DeleteUserProfile,
[Parameter()]
[String]$installDirectory
)
begin {
function Test-IsSystem {
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
return $id.Name -like "NT AUTHORITY*" -or $id.IsSystem
}
}
process {
if (-not (Test-IsSystem)) {
Write-Error -Message "This script needs to run as System (in order to hide the gui)."
exit 1
}
$PotentialInstallLocations = New-Object System.Collections.Generic.List[Object]
$ProgramFiles = Get-ChildItem $env:ProgramFiles -ErrorAction SilentlyContinue | Where-Object { $_.PSIsContainer -eq $true -and $_.Name -like "*Opera*" } | Select-Object Fullname
if ($ProgramFiles) { $ProgramFiles | ForEach-Object { $PotentialInstallLocations.Add($_) } }
$ProgramFilesX86 = Get-ChildItem ${env:ProgramFiles(x86)} -ErrorAction SilentlyContinue | Where-Object { $_.PSIsContainer -eq $true -and $_.Name -like "*Opera*" } | Select-Object Fullname
if ($ProgramFilesX86) { $ProgramFilesX86 | ForEach-Object { $PotentialInstallLocations.Add($_) } }
$AppData = Get-ChildItem C:\Users\*\AppData\Local\Programs -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.PSIsContainer -eq $true -and $_.Name -like "*Opera*" } | Select-Object Fullname
if ($AppData) { $AppData | ForEach-Object { $PotentialInstallLocations.Add($_) } }
if ($installDirectory) {
$Directory = Get-Item $installDirectory -ErrorAction SilentlyContinue
if ($Directory) { $PotentialInstallLocations.Add($Directory) }
}
if ($PotentialInstallLocations) {
$OperaExe = $PotentialInstallLocations | ForEach-Object { Get-ChildItem $_.FullName | Where-Object { $_.Name -like "opera.exe" } }
$LauncherExe = $OperaExe | ForEach-Object { "$($_.Directory)\launcher.exe" | Where-Object { Test-Path $_ -ErrorAction SilentlyContinue } }
}
if ($LauncherExe) {
Write-Host "Opera installations found! Below are the install locations."
$LauncherExe | Write-Host
# Killing All Opera Processes
Write-Warning "Killing all Opera Processes for uninstall."
Get-Process "opera" -ErrorAction SilentlyContinue | Stop-Process -Force
Get-Process "OperaSetup" -ErrorAction SilentlyContinue | Stop-Process -Force
if ($DeleteUserProfile) {
Write-Warning "Delete User Browser Profile Selected!"
$Arguments = "--uninstall", "--runimmediately", "--deleteuserprofile=1"
}
else {
$Arguments = "--uninstall", "--runimmediately", "--deleteuserprofile=0"
}
$Process = $LauncherExe | ForEach-Object {
Start-Process -Wait $_ -ArgumentList $Arguments -PassThru
}
Write-Host "Exit Code(s): $($Process.ExitCode)"
$Process | ForEach-Object {
switch ($_.ExitCode) {
0 { Write-Host "Opera removal Success! Please note that Opera will still be visible in the Control Panel however it won't prompt for admin to remove it from the control panel when clicking uninstall." }
default {
Write-Error "Exit code does not indicate success"
exit 1
}
}
}
$TaskbarLocations = Get-ChildItem -Recurse "C:\Users\*\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar" -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "Opera Browser.lnk" } | Select-Object FullName
if ($TaskbarLocations) {
Write-Host "Cleaning up taskbar icons. Please note the taskbar icon will still be visible (though it won't prompt for admin to remove it from the taskbar)."
Write-Host "### Taskbar Icon Locations ###"
$TaskbarLocations.FullName | Write-Host
$TaskbarLocations | ForEach-Object {
Remove-Item $_.FullName
}
}
else {
Write-Host "No Taskbar Icons found!"
}
$DesktopIcons = Get-ChildItem -Recurse "C:\Users\*\Desktop" -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "Opera Browser.lnk" } | Select-Object FullName
if ($DesktopIcons) {
Write-Host "Cleaning up Desktop icons."
Write-Host "### Desktop Icon Locations ###"
$DesktopIcons.FullName | Write-Host
$DesktopIcons | ForEach-Object {
Remove-Item $_.FullName
}
}
else {
Write-Host "No Desktop Icons found!"
}
$StartMenu = Get-ChildItem -Recurse "C:\Users\*\AppData\Roaming\Microsoft\Windows\Start Menu\Programs" -ErrorAction SilentlyContinue | Where-Object { $_.Name -like "Opera Browser.lnk" } | Select-Object FullName
if ($StartMenu) {
Write-Host "Cleaning up Start Menu."
Write-Host "### StartMenu Locations ###"
$StartMenu.FullName | Write-Host
$StartMenu | ForEach-Object {
Remove-Item $_.FullName
}
}
else {
Write-Host "No Start Menu Entries found!"
}
}
else {
Write-Error "No installations found in C:\Users\*\AppData\Local\Programs, C:\Program Files or C:\Program Files(x86). Maybe give an installation directory using -InstallDirectory 'C:\ReplaceMe' ?"
Exit 1
}
}