Home > Software engineering >  Use Invoke-Item calling .exe with variable folder path and passing /qn argument
Use Invoke-Item calling .exe with variable folder path and passing /qn argument

Time:12-03

I struggling to find whats wrong with my code. Im trying to uninstall an app on several Windows 10 clients, where the path to the .exe is different on any machine. I use Get-ChildItem to get that specific folder. Now im trying to execute that path with the .exe at the end and add a /qn trigger to silently deinstall. However, i always get back errors regarding the Invoke-Item function.

Here is my script:

First im trying to find the variable foldername inside the app folder:

$path_to_exe = Get-ChildItem -Path "C:\Program Files\ApplicationName" -Include "installer.exe" -Recurse | 
Where-Object { $_.FullName -match '\{(.*)\}' }

Which gives me the path as a Get-ChildItem response.

Now i convert the path given from Get-ChildItem to a "Normal" Path

$path_to_exe = Convert-Path $Path_to_exe.PSPath

Now i try to call the path i found with the /qn trigger to silently deinstall.

"`"$path_to_exe`""   " /qn" | Invoke-Item

Im 100% sure that my approach is a beginner tier one. If anyone has a better idea, please educate me.

Thanks :)

EDIT:

The manufacturer states i should use the following:

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{1D9F5D88-12AA-427F-8A33-DED71D60E4D9} - MsiExec.exe /X{1D9F5D88-12AA-427F-8A33-DED71D60E4D9}

Does anybody have an idea how i can extract that guid from the Get-ChildItem registry query?

CodePudding user response:

get-package "*applicationname*" | uninstall-package

CodePudding user response:

I finally figured it out! As i knew, i had the wrong approach. I was making it way to complicated trying to parse the EXE file to a seperate powershell instance and adding arguments. I rewrote my whole code using the msiexec function.

So first i needed to find out the GUID of the app using the Get-ChildItem function.

$Path_to_GUID = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
    Get-ItemProperty |
        Where-Object {$_.DisplayName -match "Application Name" } |
            Select-Object -Property DisplayName, UninstallString

Now i had the desired path of that Get-ChildItem object, with the .UninstallString property. Since i only want the GUID itself, i used the Split-Path function to separate the path by the folder name/GUID which in the beginning somewhat looked like this:

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{xxxxxx-xxxx-xxxx-xxxx}\installer.exe

So using the Split-Path function i first only cut the -Parent part.

$Path_to_GUID = Split-Path -Path $Path_to_GUID.UninstallString -Parent

Which gave me the path like this, without the installer.exe at the end:

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{xxxxxx-xxxx-xxxx-xxxx}

So now i only want the part of the path with the curly braces using the -Leaf argument:

$Path_to_GUID = Split-Path $Path_to_GUID -Leaf

Which gave me the desired string:

{xxxxxx-xxxx-xxxx-xxxx}

Now i just had to uninstall using msiexec and adding the silent parameter:

msiexec /x $Path_to_GUID /qn

Here's the full code (Powershell):

$Path_to_GUID = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
        Get-ItemProperty |
            Where-Object {$_.DisplayName -match "Application Name" } |
                Select-Object -Property DisplayName, UninstallString

$Path_to_GUID = Split-Path -Path $Path_to_GUID.UninstallString -Parent
$Path_to_GUID = Split-Path $Path_to_GUID -Leaf

msiexec /x $Path_to_GUID /qn
  • Related