Skip to content

Instantly share code, notes, and snippets.

@Chalcahuite
Last active December 12, 2016 16:39
Show Gist options
  • Save Chalcahuite/3e06dfb3b98152aa646739dd3eae7b0b to your computer and use it in GitHub Desktop.
Save Chalcahuite/3e06dfb3b98152aa646739dd3eae7b0b to your computer and use it in GitHub Desktop.
#!/bin/bash
# uninstallOffice2016.sh
# Script to completely uninstall Office 2016. Per this article: https://support.office.com/en-us/article/Uninstall-Office-2016-for-Mac-eefa1199-5b58-43af-8a3d-b73dc1a8cae3
# ©2016 by Sergio Aviles.
# version 1.0 2016-11-30
# version 1.1 2016-12-08 Added an "Are you sure?" prompt at the start of the script.
#Define Logging
log_location="/Library/Logs/uninstallOffice.log"
ScriptLogging()
{
DATE=$(date +%Y-%m-%d\ %H:%M:%S)
LOG="$log_location"
echo "$DATE" " $1" >> $LOG
echo "$DATE" " $1"
}
ScriptLogging "-----Uninstall Office 2016-----"
##Variables
User=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
dockutil='/usr/local/bin/dockutil'
JAMF=$(/usr/bin/which jamf)
iconPath="/Library/Application Support/KIT/Kabletown_Icons/Icon.iconset/Icon_256x256.png"
VolumeName=$(diskutil cs list | awk -F: '/Volume Name/ {print $NF}' | sed 's/ //g')
theIconPath="$VolumeName"$(/bin/echo $iconPath | sed 's/\//:/g')
Outlook='/Applications/Microsoft Outlook.app'
Word='/Applications/Microsoft Word.app'
Excel='/Applications/Microsoft Excel.app'
OneNote='/Applications/Microsoft OneNote.app'
PowerPoint='/Applications/Microsoft PowerPoint.app'
##Functions
detectDockUtil()
{
ScriptLogging "Verifying that DockUtil is installed."
if [[ -x ${dockutil} ]]; then
ScriptLogging "DockUtil installed. Proceeding."
else
ScriptLogging "DockUtil not installed. Installing."
$JAMF policy -event dockutil
ScriptLogging "DockUtil installed. Proceeding."
fi
}
#check for Comcast logo icon files
checkforIcon()
{
if [[ ! -e "$iconPath" ]]; then
ScriptLogging "Icon file missing. Downloading."
$JAMF policy -event addLogo
else
ScriptLogging "Icon file found. Proceeding."
fi
}
#Remove Office 2016 apps
removeApps()
{
OutlookPID=$(pgrep Outlook)
WordPID=$(pgrep Word)
ExcelPID=$(pgrep Excel)
OneNotePID=$(pgrep OneNote)
PowerPointPID=$(pgrep PowerPoint)
ScriptLogging "Removing Office 2016 apps."
if [[ -e "$Outlook" ]]; then
if [[ ${OutlookPID} -gt 0 ]]; then
/usr/bin/osascript -e 'quit app "'"$Outlook"'"'
fi
/usr/bin/wait 5
/bin/rm -rf "$Outlook"
ScriptLogging "Removing Outlook"
else
"Outlook not found. Skipping"
fi
if [[ -e "$Word" ]]; then
if [[ ${WordPID} -gt 0 ]]; then
/usr/bin/osascript -e 'quit app "'"$Word"'"'
fi
/usr/bin/wait 5
/bin/rm -rf "$Word"
ScriptLogging "Removing Word"
else
"Word not found. Skipping"
fi
if [[ -e "$Excel" ]]; then
if [[ ${ExcelPID} -gt 0 ]]; then
/usr/bin/osascript -e 'quit app "'"$Excel"'"'
fi
/usr/bin/wait 5
/bin/rm -rf "$Excel"
ScriptLogging "Removing Excel"
else
"Excel not found. Skipping"
fi
if [[ -e "$OneNote" ]]; then
if [[ ${OneNotePID} -gt 0 ]]; then
/usr/bin/osascript -e 'quit app "'"$OneNote"'"'
fi
/usr/bin/wait 5
/bin/rm -rf "$OneNote"
ScriptLogging "Removing OneNote"
else
"OneNote not found. Skipping"
fi
if [[ -e "$PowerPoint" ]]; then
if [[ ${PowerPointPID} -gt 0 ]]; then
/usr/bin/osascript -e 'quit app "'"$PowerPoint"'"'
fi
/usr/bin/wait 5
/bin/rm -rf "$PowerPoint"
ScriptLogging "Removing PowerPoint"
else
"PowerPoint not found. Skipping"
fi
pkg=$(/usr/sbin/pkgutil --pkgs | grep com.microsoft.package)
for p in $pkg; do
ScriptLogging "Removing Package receipt for $p"
/usr/sbin/pkgutil --forget "$p"
done
/usr/sbin/pkgutil --forget com.microsoft.pkg.licensing
/usr/sbin/pkgutil --forget com.microsoft.pkg.licensing.volume
}
cleanupDock()
{
ScriptLogging "Removing Office 2016 apps from Dock...."
if [[ $($dockutil --find 'Microsoft Outlook' /Users/$User >/dev/null; echo $?) -eq 0 ]]; then
$dockutil --remove 'Microsoft Outlook' --no-restart /Users/$User
fi
if [[ $($dockutil --find 'Microsoft Word' /Users/$User >/dev/null; echo $?) -eq 0 ]]; then
$dockutil --remove 'Microsoft Word' --no-restart /Users/$User
fi
if [[ $($dockutil --find 'Microsoft Excel' /Users/$User >/dev/null; echo $?) -eq 0 ]]; then
$dockutil --remove 'Microsoft Excel' --no-restart /Users/$User
fi
if [[ $($dockutil --find 'Microsoft OneNote' /Users/$User >/dev/null; echo $?) -eq 0 ]]; then
$dockutil --remove 'Microsoft OneNote' --no-restart /Users/$User
fi
if [[ $($dockutil --find 'Microsoft PowerPoint' /Users/$User >/dev/null; echo $?) -eq 0 ]]; then
$dockutil --remove 'Microsoft PowerPoint' /Users/$User
fi
}
removeLicensing()
{
if [[ -e /Library/LaunchDaemons/com.microsoft.office.licensingV2.helper.plist ]]; then
ScriptLogging "Removing licensing."
/bin/launchctl unload -w /Library/LaunchDaemons/com.microsoft.office.licensingV2.helper.plist
/bin/rm -f /Library/LaunchDaemons/com.microsoft.office.licensingV2.helper.plist
/bin/rm -r /Library/PrivilegedHelperTools/com.microsoft.office.licensingV2.helper
else
ScriptLogging "Licensing file missing. Skipping."
fi
}
promptUser()
{
ScriptLogging "Prompting to see if user wants to delete their Office/Outlook Data."
toDelete=$(/usr/bin/sudo -u "$User" /usr/bin/osascript -e 'Tell application "System Events" to display dialog "Do you want to delete your Outlook mail and other data?" with title "Kabletown IT - Confirm Data Removal" with icon file "'"$theIconPath"'" with text buttons {"Yes","No"} default button 1' -e 'button returned of result')
}
removeUserData()
{
if [[ "$toDelete" = "Yes" ]]; then
ScriptLogging "Removing user data from ~/Library/Containers/."
/bin/rm -rf "/Users/$User/Library/Containers/com.microsoft.errorreporting"
/bin/rm -rf "/Users/$User/Library/Containers/com.microsoft.Excel"
/bin/rm -rf "/Users/$User/Library/Containers/com.microsoft.netlib.shipassertprocess"
/bin/rm -rf "/Users/$User/Library/Containers/com.microsoft.Office365ServiceV2"
/bin/rm -rf "/Users/$User/Library/Containers/com.microsoft.Outlook"
/bin/rm -rf "/Users/$User/Library/Containers/com.microsoft.PowerPoint"
/bin/rm -rf "/Users/$User/Library/Containers/com.microsoft.RMS-XPCService"
/bin/rm -rf "/Users/$User/Library/Containers/com.microsoft.Word"
/bin/rm -rf "/Users/$User/Library/Containers/com.microsoft.onenote.mac"
ScriptLogging "Removing user data from ~/Library/Group Containers/."
/bin/rm -rf "/Users/$User/Library/Group Containers/UBF8T346G9.ms"
/bin/rm -rf "/Users/$User/Library/Group Containers/UBF8T346G9.Office"
/bin/rm -rf "/Users/$User/Library/Group Containers/UBF8T346G9.OfficeOsfWebHost"
else
ScriptLogging "User data will not be removed. Skipping."
fi
HOME="/Users/$User"
userKeychain=$HOME/Library/Keychains/login.keychain
if [[ $(/usr/bin/security find-generic-password -l Exchange $userKeychain) ]]
then
exchangePasswordClearStatus=0
until (( $exchangePasswordClearStatus == 1 ))
do
ScriptLogging "Looking for existing entry for exchange password..."
if [[ $(/usr/bin/security find-generic-password -l Exchange $userKeychain) ]]
then
ScriptLogging "Found an existing password in user keychain for exchange. Attempting to delete..."
/usr/bin/security delete-generic-password -l Exchange $userKeychain
if (( $? == 0 ))
then
ScriptLogging "Exchange password deleted"
fi
else
ScriptLogging "Did NOT find any more passwords in user keychain for Exchange."
exchangePasswordClearStatus=1
fi
done
else
ScriptLogging "Did not find a saved password for Exchange"
fi
}
verifyProcedure()
{
ScriptLogging "Prompting user to confirm the uninstall of Office 2016."
confirm=$(/usr/bin/sudo -u "$User" /usr/bin/osascript -e 'Tell application "System Events" to display dialog "Are you sure you want to delete Office 2016 and its data from your Mac?" with title "Kabletown IT - Confirm Office Removal" with icon file "'"$theIconPath"'" with text buttons {"Yes","No"} default button 1' -e 'button returned of result')
if [[ "$confirm" = "No" ]]; then
ScriptLogging "User aborted script."
ScriptLogging "-----End-----"
exit 0
else
ScriptLogging "User opted to proceed. Continuing."
fi
}
main()
{
detectDockUtil
checkforIcon
verifyProcedure
removeApps
removeLicensing
promptUser
removeUserData
cleanupDock
}
##Execute
main
ScriptLogging "-----End-----"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment