We help IT Professionals succeed at work.
Private
Troubleshooting Question

Powershell Extract Uninstall String

credog
credog asked
on
22 Views
Last Modified: 2020-10-26
I'm trying to make an uninstall script a bit more robust by extracting the uninstall string and using it to uninstall a program instead of using a static location.  I can get the uninstall command with the following command, however I'm having some trouble just getting the string itself since it is in some xml data within the SwidTagText field:
PS: Get-Package -Provider Programs -IncludeWindowsInstaller -Name "Microsoft OneDrive" | select -ExpandProperty SwidTagText

<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<SoftwareIdentity
  name="Microsoft OneDrive"
  version="20.143.0716.0003"
  versionScheme="unknown" xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd">
  <Meta
    DisplayName="Microsoft OneDrive"
    DisplayIcon="C:\Users\bob\AppData\Local\Microsoft\OneDrive\20.143.0716.0003_2\OneDriveSetup.exe,-101"
    DisplayVersion="20.143.0716.0003"
    HelpLink="http://go.microsoft.com/fwlink/?LinkID=215117"
    Publisher="Microsoft Corporation"
    UninstallString="C:\Users\bob\AppData\Local\Microsoft\OneDrive\20.143.0716.0003_2\OneDriveSetup.exe  /uninstall "
    UrlUpdateInfo="http://go.microsoft.com/fwlink/?LinkID=223554"
    EstimatedSize="150145"
    NoRepair="1"
    NoModify="1" />
</SoftwareIdentity>

In a one liner I would like to grab out "C:\Users\bob\AppData\Local\Microsoft\OneDrive\20.143.0716.0003_2\OneDriveSetup.exe  /uninstall" to a variable to use to uninstall the program.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
No need to dive into the XML here, you can get it directly from SwidTags.Metadata:
$uninstallString = (Get-Package -Provider Programs -IncludeWindowsInstaller -Name "Microsoft OneDrive").SwidTags.Metadata['UninstallString']

Open in new window

Author

Commented:
Thank you.  That worked great. However when I try this on a path like:
C:\Program Files (x86)\Microsoft OneDrive\20.169.0823.0006\OneDriveSetup.exe  /uninstall  /allusers
I get the following error: "The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program."  Seems like it needs to be quoted (single maybe), but not sure how to do that when used as part of a oneliner:
...; Invoke-Expression $uninstallString}
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
I'm sure I'm missing something.  I want to use the variable value directly as it's returned when the following is run:
$uninstallString = (Get-Package -Provider Programs -IncludeWindowsInstaller -Name "Microsoft OneDrive").SwidTags.Metadata['UninstallString']
On one system it returns the following exactly has shown here:
C:\Users\bob\AppData\Local\Microsoft\OneDrive\20.143.0716.0003_12\OneDriveSetup.exe  /uninstall
I use that in the following oneliner in an ISE window and it seems to work:
$output = test-path ($uninstallString -replace '/.*') ; if ($output -ne "True"){ $result='Removal path not found' } else { $result= 'Removing Onedrive'; Invoke-Expression $uninstallString}; $result
When I use it on another system it returns the following exactly as shown below:
C:\Program Files (x86)\Microsoft OneDrive\20.169.0823.0006\OneDriveSetup.exe  /uninstall  /allusers
When the variable is used it in the same oneliner above ($output = test-path ($uninstallString......) I get the following error and it doesn't remove the program:
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:19
+ C:\Program Files (x86)\Microsoft OneDrive\20.169.0823.0006\OneDriveSe ...
+                   ~~~
    + CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
I also tried using & instead of invoke-expression and that didn't work. I'm trying the get the variable to work when the path is within Program FIles (x86) .....  Is there another step I need to perform on the value in the variable to use in the oneliner?
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions