Page 1 of 2

Using PowerShell to detect and remove certain type of software?

Posted: Tue Dec 11, 2018 5:06 pm
by ITEngineer
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.

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
}
Any help would be greatly appreciated.

Thanks,

Re: Using PowerShell to detect and remove certain type of software?

Posted: Tue Dec 11, 2018 5:31 pm
by jvierra
You have to tell us what doesn't work. What are the errors.

The code you have could only have worked under very restrictive circumstances if at all.

If you search the MS site you will find the official MS Office cleaner tool which will remove all versions of Office.

Re: Using PowerShell to detect and remove certain type of software?

Posted: Tue Dec 11, 2018 5:41 pm
by ITEngineer
jvierra wrote: Tue Dec 11, 2018 5:31 pm You have to tell us what doesn't work. What are the errors.

The code you have could only have worked under very restrictive circumstances if at all.

If you search the MS site you will find the official MS Office cleaner tool which will remove all versions of Office.

Code: Select all

You cannot call a method on a null-valued expression.
At line:41 char:33
+ $uninstall.UninstallString.Split <<<< ('{}')[1]
    + CategoryInfo          : InvalidOperation: (Split:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvokeMethodOnNull
Does the Office Cleaner tool can be scripted to be executed in bulk for multiple computers remotely?

Re: Using PowerShell to detect and remove certain type of software?

Posted: Tue Dec 11, 2018 8:54 pm
by jvierra
As I noted, the code as posted cannot work. It does not do anything you expect from what I see. It attempts to uninstall everything.

You try to uninstall everything that matches and doesn't math O365. That includes all software.

The following logic includes all things found:

if ($uninstall.DisplayName -like "*365*") {
} ElseIf ($uninstall.DisplayName -notlike '*365*') {
}


The code you have was originally designed to install one office product but seems to have been changed badly.

Re: Using PowerShell to detect and remove certain type of software?

Posted: Wed Dec 12, 2018 3:12 am
by ITEngineer
jvierra wrote: Tue Dec 11, 2018 8:54 pm As I noted, the code as posted cannot work. It does not do anything you expect from what I see. It attempts to uninstall everything.

You try to uninstall everything that matches and doesn't math O365. That includes all software.

The following logic includes all things found:

if ($uninstall.DisplayName -like "*365*") {
} ElseIf ($uninstall.DisplayName -notlike '*365*') {
}


The code you have was originally designed to install one office product but seems to have been changed badly.
Hi Mr. Vierra,

yes, the goal of the script is to uninstall the Software that is matching the $AppDisplayName variable.
in this example, I need to uninstall Microsoft office.

Re: Using PowerShell to detect and remove certain type of software?

Posted: Wed Dec 12, 2018 5:54 pm
by jvierra
Here is a rethink of what you are doing. It can help you to discover your issue. It is tested. I am posting it to help you get an idea of how you have to approach a problem like this. It will not remove anything until you change the "$ShouldProcess" variable.

Re: Using PowerShell to detect and remove certain type of software?

Posted: Wed Dec 12, 2018 9:01 pm
by ITEngineer
jvierra wrote: Wed Dec 12, 2018 5:54 pm Here is a rethink of what you are doing. It can help you to discover your issue. It is tested. I am posting it to help you get an idea of how you have to approach a problem like this. It will not remove anything until you change the "$ShouldProcess" variable.
Mr. Vierra,

Thanks for the help,
however, the script run with the error below:

Code: Select all

The "=" operator is missing after a named argument.
At line:7 char:29

Missing function body in function declaration.
At line:17 char:2

Missing function body in function declaration.
At line:23 char:2

Re: Using PowerShell to detect and remove certain type of software?

Posted: Wed Dec 12, 2018 9:36 pm
by jvierra
Works perfectly for me. You must have damaged the file in some way.

Download this again and don't make any changes to the file before running it.

Re: Using PowerShell to detect and remove certain type of software?

Posted: Wed Dec 12, 2018 9:45 pm
by ITEngineer
jvierra wrote: Wed Dec 12, 2018 9:36 pm Works perfectly for me. You must have damaged the file in some way.

Download this again and don't make any changes to the file before running it.
Is it because I'm executing it under Windows 7 Test PC:

Code: Select all

PS C:\Windows\system32> $PSVersionTable

Name                           Value                                                                                                                                                                          
----                           -----                                                                                                                                                                          
CLRVersion                     2.0.50727.8762                                                                                                                                                                 
BuildVersion                   6.1.7601.17514                                                                                                                                                                 
PSVersion                      2.0                                                                                                                                                                            
WSManStackVersion              2.0                                                                                                                                                                            
PSCompatibleVersions           {1.0, 2.0}                                                                                                                                                                     
SerializationVersion           1.1.0.1                                                                                                                                                                        
PSRemotingProtocolVersion      2.1    
The error is still:

Code: Select all

The "=" operator is missing after a named argument.
At C:\Users\Admin\Desktop\Test-RemoveOffice (1).ps1:7 char:30
+         [Parameter(Mandatory) <<<< ]
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEqualsInNamedArgument

Re: Using PowerShell to detect and remove certain type of software?

Posted: Wed Dec 12, 2018 10:03 pm
by jvierra
PowerShell 2 is no longer supported by Microsoft. It is considered a security problem. You need to upgrade.

PS does not support most of the extensions to PS at V3 and later. Just add the "= $true" to the "[Parameter(" statement

[Parameter(Mandatory=$true)]