Using PowerShell to detect and remove certain type of software?

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

Using PowerShell to detect and remove certain type of software?

Post by ITEngineer » Tue Dec 11, 2018 6: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.

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,
/* IT Engineer */

User avatar
jvierra
Posts: 13490
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Tue Dec 11, 2018 6: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.

User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Tue Dec 11, 2018 6:41 pm

jvierra wrote:
Tue Dec 11, 2018 6: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?
/* IT Engineer */

User avatar
jvierra
Posts: 13490
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Tue Dec 11, 2018 9: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.

User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Wed Dec 12, 2018 4:12 am

jvierra wrote:
Tue Dec 11, 2018 9: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.
/* IT Engineer */

User avatar
jvierra
Posts: 13490
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Dec 12, 2018 6: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.
Attachments
Test-RemoveOffice.ps1
(2.3 KiB) Downloaded 14 times

User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Wed Dec 12, 2018 10:01 pm

jvierra wrote:
Wed Dec 12, 2018 6: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
/* IT Engineer */

User avatar
jvierra
Posts: 13490
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Dec 12, 2018 10: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.
Attachments
Test-RemoveOffice.ps1
(2.32 KiB) Downloaded 12 times

User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

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

Post by ITEngineer » Wed Dec 12, 2018 10:45 pm

jvierra wrote:
Wed Dec 12, 2018 10: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
/* IT Engineer */

User avatar
jvierra
Posts: 13490
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Dec 12, 2018 11:03 pm

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)]

Locked