Worklet for Uninstalling Office 365 Click to Run


Userlevel 3
Badge

I had a need to remove the pre-installed Office 365 components. Thought I would share. Its a little messy and unrefined, but I tested it and its working. I’ll post update after I clean it up a little. 

Evaluation

<#
.SYNOPSIS
Uninstall Application by Name - Evaluation Script
OS Support: Windows 7 and above
Required modules: NONE
.DESCRIPTION
This worklet is designed to grant an Admin the ability to uninstall an application with minimal knowledge of the bitness,
installer type, or uninstall command line. By placing an application name between the single quotes, the worklet will
scan the registry for the matching application. If the application is found, it will exit with an Exit code of '1' and
flag the device for remediation.

Usage:
$appName: The application name provided must match exactly as it is displayed in "Programs and Features" (Add or
Remove Programs) for Win7/8.1, and "Apps and Features" for Win10. Universal Windows Platform (UWP) applications are
not currently supported in this worklet.

Additional Notes:
Some software (such as M365 Apps) installs as a "suite" even though each component could have its own registry entry.
This type of installation is not supported with this worklet. For more information please see the software vendor's
support documentation.
.EXAMPLE
$appName = '7-Zip 19.00 (x64)'
.EXAMPLE
$appName = 'Microsoft Silverlight'
.NOTES
Author: eliles,rrodriguez
Date: February 24, 2021
#>

# Using scriptblock to relaunch in native environment for 64bit detection.
$scriptBlock = {

######## Make changes within the block ########
# Add Application name exactly as it appears in Add/Remove Programs, Programs and Features, or Apps and Features between single quotes.
$appName = 'Microsoft 365*'
###############################################

# Define registry location for uninstall keys
$uninstReg = @('HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')

# Get all entries that match our criteria. DisplayName matches $appName (using -like to support special characters)
$installed = @(Get-ChildItem $uninstReg -ErrorAction SilentlyContinue | Get-ItemProperty | Where-Object {($_.DisplayName -like $appName)})

# If any matches were found, $installed will return a "1" and pass it to $exitCode flagging the device for remediation.
if ($installed)
{
return 1
}
else
{
return 0
}
}

# Execution of $scriptBlock
$exitCode = & "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptblock

# Exit with value provided by $installed
Exit $exitCode

Remediation Code

$scriptBlock = {
$appName = '*OfficeClickToRun.exe*'
###############################################

# Define registry location for uninstall keys
$uninstReg = @('HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')

# Get all entries that match our criteria. DisplayName matches $appName (using -like to support special characters)
$installed = @(Get-ChildItem $uninstReg -ErrorAction SilentlyContinue | Get-ItemProperty | Where-Object {($_.UninstallString -like $appName)})
foreach ($crap in $installed) {
$arg = $crap.uninstallstring.substring(81).tostring() + " DisplayLevel=False"
Start-Process "C:\Program Files\Common Files\Microsoft Shared\ClickToRun\OfficeClickToRun.exe" -ArgumentList "$arg" -wait
}

}

# Execution of $scriptBlock
$exitCode = & "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptblock

# Exit with value provided by $installed
Exit $exitCode

 


3 replies

WORKS GREAT!

I just used this code to build a worklet and ran it against a couple of test PCs with Office 365 installed and it worked perfectly! All Office apps and folders are gone from the “microsoft shared” directory and the start menu.  Thanks so much for your time and effort you put into writing this, David!

-Russ N.

Userlevel 3
Badge

Glad this was helpful. I did a lot of looking around to find a good way to do this and the only thing I could find was an insanely complex vb script. Mine does what I need it to and I incorporated it into our remove bloatware worklet. When I have more time I will update it to use split or something to be more dynamic in pulling the substring out of the uninstall key. That way if Microsoft changes the uninstall key, it should compensate. 

Userlevel 3
Badge

Microsoft has made some changes to office 365 and I was noticing that sometimes my script would hang trying to uninstall. I found a better way. Use the “Enterprise version of Microsoft Support and Recovery Assistant” . Download the PS1 script file under “Sample 1: Script to run a non-interactive session”. Edit line 72 to be like this:

$SaraScenarioArgument = "-S OfficeScrubScenario -Script -AcceptEula"

Attach the ps1 file to your worklet and below is your remediation code. 

$exitCode = & "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -File "ExecuteSaraCmd.ps1"

# Exit with value provided by $installed
Exit $exitCode

P.S. This useful utility will remove ALL versions of Microsoft Office, not just preinstalled office 365 bloatware. 

Reply