Uninstall program with powershell and invoke-command when quotes and arguments are involved RRS feed

  • Question

  • Hi,

    Since win32_product has issues (between not returning the full list of programs and encurring a repair of applications) I ve been looking at the registry to get applications matching a certain expression and in-installing it.

    I have been able to obtain the uninstall commands from the registry. But if I try to run said command nothing happens. One of the main reason for that is the way quotes are handled.

    For instance, the Registry for the Wireshark application contains QuietUninstallString with a value of:

    "C:\Program Files (x86)\Wireshark\uninstall.exe" /S

    Quotes and everything.

    So the thing is, that value is stored in a variable $ucmd

    But I cannot seem to get Invoke-Command or Invoke-Command including Invoke-Expression to be able to execute that command.

    So the question is, how do you run a program that has spaces in its path and also contains arguments outside of the path such as above.


    Examples of scripts and errors:


    1/ Try:

    Invoke-Command -ComputerName $server -ScriptBlock { &$using:ucmd}


    The term '"C:\Program Files (x86)\Wireshark\uninstall.exe" /S' is not recognized as the name of a cmdlet, function, script file, or operable program.


    2/ Try:

    Invoke-Command -ComputerName $server -ScriptBlock { &cmd /c $using:ucmd}


    Nothing at all, no output and no program uninstalled.


    So forth and so forth

    • Edited by O.Ragain Thursday, February 22, 2018 6:57 PM
    Thursday, February 22, 2018 6:03 PM

All replies

  • $uninstallStr = '"C:\Program Files (x86)\Wireshark\uninstall.exe" /S'
    Invoke-Expression $uninstallStr


    • Proposed as answer by PRASOON KARUNAN V Thursday, February 22, 2018 6:43 PM
    • Unproposed as answer by O.Ragain Thursday, February 22, 2018 7:21 PM
    Thursday, February 22, 2018 6:10 PM
  • I should clarify that my $ucmd is obtained by using:

    $ucmd  = ($RegBase.OpenSubKey("$CurrentReg$_")).GetValue($Uninstall)

    It is not something I manually type.

    If I print what is in $ucmd I get:

    "C:\Program Files (x86)\Wireshark\uninstall.exe" /S

    And if I execute on the remote system:

    Invoke-Command -ComputerName $server -ScriptBlock {Invoke-Expression $using:ucmd}

    I get the following errors:

    At line:1 char:51
    + "C:\Program Files (x86)\Wireshark\uninstall.exe" /S
    +                                                   ~
    You must provide a value expression following the '/' operator.
    At line:1 char:51
    + "C:\Program Files (x86)\Wireshark\uninstall.exe" /S
    +                                                   ~
    Unexpected token 'S' in expression or statement.
        + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
        + FullyQualifiedErrorId : ExpectedValueExpression,Microsoft.PowerShell.Commands.InvokeExpressionCommand
        + PSComputerName        : DO-SVP-DCO-03.instream.local

    Thursday, February 22, 2018 6:36 PM
  • take a look of 


    get-package Whireshark* | remove-package


    Thursday, February 22, 2018 6:44 PM
  • Chris,

    We don't have the package management module installed on our servers. And even if we try to install it, it does not find the wireshark application currently installed.


    Thursday, February 22, 2018 6:57 PM
  • Package Manager can only manage packages that can be installed by package manager.  Classic setup and MSI cannot be managed.


    Thursday, February 22, 2018 7:32 PM
  • $ucmd  = ($RegBase.OpenSubKey("$CurrentReg$_")).GetValue($Uninstall) -replace '"'


    Thursday, February 22, 2018 7:34 PM
  • $ucmd = '"C:\Program Files (x86)\Wireshark\uninstall.exe" /S' -split '"\s+'
    start-process $ucmd[0] -ArgumentList $ucmd[1]


    Thursday, February 22, 2018 7:43 PM