Link to home
Start Free TrialLog in
Avatar of credog
credog

asked on

Powershell Extract Uninstall String

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>

Open in new window

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.
Avatar of oBdA
oBdA

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

Avatar of credog

ASKER

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

Open in new window

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}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of credog

ASKER

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

Open in new window

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

Open in new window

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

Open in new window

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

Open in new window

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

Open in new window

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial