Conor Timms
Conor Timms

Reputation: 13

Powershell outputting string but not executing

Powershell outputs the string instead of executing command.

$apps = @()
$apps += Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" # 32 Bit
$apps += Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*"  

$apps365 = $apps | Where-Object {$_.DisplayName -Match "365" | Sort-Object DisplayName}



$uninPaths = @()
foreach ($item in $apps365) {
    $uninPaths += "& CMD /C "+$item.UninstallString
}

Invoke-Command -ScriptBlock{$uninPaths}

Upvotes: 0

Views: 43

Answers (1)

Cpt.Whale
Cpt.Whale

Reputation: 5341

$uninPaths is just a string. To execute it like a command, you can feed it to Invoke-Expression:

Invoke-Expression -Command $uninPaths

Invoke-Command is generally used for running powershell commands on remote machines.

Upvotes: 1

Related Questions