Application Packaging, PowerShell

Running an .exe from a path with a wildcard in PowerShell

It’s been a while since I have posted anything and there is no shortage of topics for me to catch up on. I am going to start with a problem I was working on the last couple of days. Here is the scenario:

I was tasked with packaging the installation of an application that was previously installed manually by the users of the environment I am working in. When the users installed the application from the vendor’s website it would get installed in each user’s appdata folder. The version I was given to package was designed as a per-system install. They also wanted me to remove the per-user install before installing the new per-system version. After creating the package for the new version, the issue became being able to uninstall the per-user version. The install was not an MSI and the vendor’s method for uninstalling was an uninstall.exe file located in the appdata folder where the application had been installed for the user. This is obviously tricky because depending on which user installed the application it could be located in any random user’s folder path. I’ve been using PowerShell for my primary scripting language for most application deployments for some time now, so the key was finding a way to do this in PS. My solution was as follows:

powershell_5-0_icon

First I set a variable for the path with my wildcard. I used the cmdlet resolve-path so that the variable would have the value of whichever user had installed the application I was trying to uninstall by searching for the uninstall.exe in the application folder.

$AppUserPath = Resolve-Path "C:\users\*\AppData\Local\AppVendor\AppName\Uninstall.exe"

Then I created an if statement that would test for the resolved path if it existed and the remove it


If (Test-Path $AppUserPath) { Start-Process -FilePath $AppUserPath -ArgumentList "/S"}

So all together it looked like this:


$AppUserPath = Resolve-Path "C:\users\*\AppData\Local\AppVendor\AppName\Uninstall.exe"
If (Test-Path $AppUserPath)
{Start-Process -FilePath $AppUserPath -ArgumentList "/S"}

So now when my script runs, it will parse through all the user appdata folders on the machine and find the one with the application installed that has uninstall.exe file. Then it will execute the uninstall.exe file using the resolved path that it found. After that installing the new per-system version was a piece of cake

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s