Using PowerShell to detect and remove certain type of software?
Posted: Tue Dec 11, 2018 5:06 pm
Hi All,
I need some help in modifying the below script that used to be working, but now it is not working.
The purpose of this script is to uninstall and remove all Microsoft Office 2010, 2013, 2016 any version (Standard or Professional) 32 and 64 bit.
Write it to the log file when failed.
Any help would be greatly appreciated.
Thanks,
I need some help in modifying the below script that used to be working, but now it is not working.
The purpose of this script is to uninstall and remove all Microsoft Office 2010, 2013, 2016 any version (Standard or Professional) 32 and 64 bit.
Write it to the log file when failed.
Code: Select all
$AppDisplayName = 'Office'
$TARGETDIR = 'C:\logs\'
$LOGFILE = 'C:\logs\script.log'
$uninstallLog = 'C:\logs\uninst.log'
if (!(Test-Path -Path $TARGETDIR )) {
New-Item -ItemType directory -Path $TARGETDIR
}
function WriteLog {
#Adds info to log file with time stamp
Param ([String] $Message)
$tStamp = Get-Date
$Day = $($tStamp).Day
$Month = $($tStamp).Month
$Year = $($tStamp).Year
$Hour = $($tStamp).Hour
$Minute = $($tStamp).Minute
Write-Output "[$Day\$Month\$Year $Hour`:$Minute] $Message" >> $LOGFILE
}
function getUninstallInfo {
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
Get-ItemProperty |
Where-Object {$_.DisplayName -like "*$($AppDisplayName)*" } |
Select-Object -Property DisplayName, DisplayVersion, UninstallString
}
$uninstall = getUninstallInfo
Try {
if ($uninstall.DisplayName -like "*365*") {
Write-Host "$($ENV:COMPUTERNAME) is already using O365 , no need to uninstall" -ForegroundColor Yellow
WriteLog "$($ENV:COMPUTERNAME) is already using O365 , no need to uninstall"
}
ElseIf ($uninstall.DisplayName -notlike '*365*') {
WriteLog "Non Office 365 Detected - Querying Uninstall command"
$productCode = "{$($uninstall.UninstallString.Split('{}')[1])}"
Write-Output $productcode
WriteLog "Uninstall command detected as $($uninstall.UninstallString) Attempting silent uninstall"
$MSIArgs = @(
'/x'
$productCode
"/qn"
"/norestart"
"/L*v"
$uninstallLog
)
Start-Process 'msiexec.exe' -ArgumentList $MSIArgs -Wait -NoNewWindow
}
}
Catch {
Throw
$message = "Error: $($_.Exception.Message)"
Write-Host $message -ForegroundColor Red -BackgroundColor DarkBlue
$message | Out-File -Append -Path $LOGFILE
}
Thanks,