I’ve recently been working on a lot of app packaging, and I decided to take the opportunity to update my scripts. In this post I’ll cover 3 PowerShell functions that I found to be the most useful. The functions can be found on my GitHub.
Invoke-MsiExec
This function works with msiexec.exe. The function allows for installing or uninstalling a .msi based application.
The function has a number of parameters:
- FilePath – The path to the .msi or .msp file
- Guid – The GUID of the MSI-based application to be uninstalled
- Install – Indicates that the /i switch should be used when calling msiexec.exe
- Uninstall – Indicates that the /x switch should be used when calling msiexec.exe
- Patch – Indicates that the /p switch should be used when calling msiexec.exe
- Arguments – A comma separated list of arguments to pass to msiexec.exe
- ExitCodes – A comma separated list of non-standard exit codes
Some notes about the parameters:
- The Install, Uninstall, and Patch parameters are mutually exclusive. Only specify 1 of these switches. The function will tell you if you accidentally specify more than 1.
- The list of arguments must be contained within single quotes and each argument must be separated by a comma. Double quotes can be passed in the arguments list. PowerShell variables can also be passed in the arguments list. The variables will be expanded inside the function.
- By default the function will only interpret exit codes 0 and 3010 as success. If the application in question has other successful exit codes, they must be specified with the ExitCodes parameter. The list of exit codes must be contained within single quotes and each exit code must be separated by a comma.
Some examples of using the function:
#Install a .msi file
Invoke-MsiExec -Install -FilePath "$PSScriptRoot\Setup.msi" -Arguments '/qn,/norestart'
#Install a .msi file with arguments containing double quotes, variables, and non-standard exit codes
Invoke-MsiExec -Install -FilePath "$PSScriptRoot\Setup.msi" -Arguments '/qn,/norestart,TRANSFORMS="$PSScriptRoot\Settings.mst"' -ExitCodes '4,8,10'
#Install a .msp file
Invoke-Msiexec -Patch -FilePath "$PSScriptRoot\Patch.msp" -Arguments '/qn,/norestart'
#Uninstall a application
Invoke-MsiExec -Uninstall -Guid "{00000000-0000-0000-0000-000000000000}" -Arguments '/qn,/norestart'
Invoke-Executable
This function works with .exe files. The function allows for running an exe with arguments.
The function has a few parameters. The same notes above about the Arguments and ExitCodes parameters also apply here.
- FilePath – The path to the .exe file
- Arguments – A comma separated list of arguments to pass to the executable
- ExitCodes – A comma separated list of non-standard exit codes
Some examples of using the function:
#Run a .exe file
Invoke-Executable -FilePath "$PSScriptRoot\Setup.exe" -Arguments '/S'
#Run a .exe file with arguments containing double quotes, and non-standard exit codes
Invoke-Executable -FilePath "$PSScriptRoot\Setup.exe" -Arguments '/S,/v"/qn REBOOT=reallysuppress"' -ExitCodes '4,8,10'
New-RegistryValue
This function allows for the creation and/or modification of registry values.
The function has a few parameters:
- RegKey – The path to the target registry key
- Name – The name of the registry value to create of modify
- PropertyType – The type of registry value to create or modify
- Value – The data to write to the registry value
Some notes about this function:
- If the specified registry key does not yet exist, the function will create it before creating the value.
- To modify the default value of a registry key, specify ‘(Default)’ in the name parameter
- Valid values for the PropertyType parameter are – ( ‘String’ ‘ExpandString’ ‘Binary’ ‘DWord’ ‘MultiString’ ‘Qword’ ‘Unknown’ )
- By default only the HKLM and HKCU registry hives are loaded. Use New-PSDrive to load other registry hives. ( New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT )
Some examples of using the function:
#Create a new Dword registry value
New-RegistryValue -RegKey "HKLM:\SOFTWARE\Example" -Name "ExampleValue" -PropertyType Dword -Value 2
#Modify the default value of a registry key
New-RegistryValue -RegKey "HKLM:\SOFTWARE\Example" -Name '(Default)' -PropertyType String -Value "abcd"