I have been getting the uninstall string via below script.
But I am getting an output like this.
MsiExec.exe /I{629388F2-A011-4F5C-A6BF-98A80A25317C}
My desired output:
{629388F2-A011-4F5C-A6BF-98A80A25317C}
After desired output , I will assign this value to the variable such as $uninstallString. And I will use it inside my doRemoveMSI function like below.
$paths = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall', 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
Get-ChildItem $paths |
Where-Object{ $_.GetValue('DisplayName') -match 'APP' } |
ForEach-Object{
$uninstallString = $_.GetValue('UninstallString')
$uninstallString
}
doRemoveMSI -msi "msiexec.exe" -arguments '/x', '$uninstallString', '/quiet', 'REBOOT=R', '/L*V "C:\msilog.log"'
As an aside:
If you're using Windows PowerShell and only need to remove MSI-installed applications, a much more convenient solution is to use
Uninstall-Package, as js2010 points out; e.g.,Get-Package -ProviderName msi *App* | Uninstall-PackageIf you're looking to execute
UninstallString/QuietUninstallStringcommand lines extracted from the registry as-is, see this answer.Your question can be reduced to how to extract a
{...}substring from a larger string, which you can do with PowerShell's regex-based-replaceoperator:Note:
{and}are situationally regex metacharacters, but are treated literally here, because they aren't part of valid quantifier subexpressions (such aso{2,3}). You may choose to always escape them as\{and\}, to be safe.Alternatively, you could use the equally regex-based
-splitoperator, and split the string into tokens by{and}, including those separators, with the help of look-around assertions, and extracting the 2nd token ([1]):See this regex101.com page for a detailed explanation of the regex and the ability to experiment with it. Note that the look-around assertions only match character positions, not actual characters, which is what enables including
{and}in the tokens returned by-split.Or the
.Split()method of the .NET[string]type, passing an array of (literal) characters to split by, using an expandable (double-quoted) string ("...") with the$(...), the subexpression operator to surround the extracted token in{and}again.Speaking of expandable strings:
The
'$uninstallString'in yourdoRemoveMSIcall,won't work as intended, because NO expansion (string interpolation) happens inside
'...', i.e. verbatim (single-quoted) strings.While
"$uninstallString"avoids that problem, you do not even need the"..."(except in cases where you need explicit to-string conversion), and can simply pass$uninstallStringas-is.As for how you're partitioning the arguments you're passing to
doRemoveMSI(consider renaming the function to conform to PowerShell's naming conventions, e.g.,Uninstall-Msi):'/L*V "C:\msilog.log"'- it is ultimately simpler to pass a single string overall, i.e. to declare your-argumentsparameter as type[string], and then invoke the function as follows: