I am trying to write a script to remotely find Microsoft Office applications (from a list), and then uninstall them to prepare for installing Office 365. I have written the following so far, which seems to work (or at least doesn’t throw any errors), but it doesn’t uninstall anything. Can anyone take a look and tell me what I have wrong? Thanks in advance!
#Gather hostname of remote PC
$PC = Read-Host "Enter the hostname to uninstall from"
IF ($Cred -eq $Null) {$Cred = Get-Credential "$env:USERDOMAIN\$env:USERNAME"}
#Enable RemoteRegistry service
Get-Service RemoteRegistry -ComputerName $PC | start-service
#Enable WinRM service
Get-Service WinRM -ComputerName $PC | Start-Service
#Disable remote firewall
PsExec -accepteula \\$($PC) netsh advfirewall set allprofiles state off
#Enter remote session
Enter-PSSession -ComputerName $PC -Credential $Cred
#Uninstall old versions
$OfficeApps = Get-WmiObject -Class Win32_Product | Where-Object {
$_.Name -match "Microsoft Office" -or
$_.Name -match "Microsoft Office Professional Plus 2010" -or
$_.Name -match "Microsoft Office Professional Plus 2013" -or
$_.Name -match "Microsoft Office Standard 2007" -or
$_.Name -match "Microsoft Office Standard 2010"
}
ForEach ($App in $OfficeApps) {
Write-Host "Uninstalling" $App.Name
$App.Uninstall()
}
#Close remote session
Exit-PSSession