【问题标题】:Powershell outputting string but not executingPowershell输出字符串但不执行
【发布时间】:2026-01-13 17:30:01
【问题描述】:

Powershell 输出字符串而不是执行命令。

$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}

【问题讨论】:

  • 是什么让您相信卸载字符串需要在cmd.exe 中运行?通常 unistall 字符串是一个标准的可执行文件,它通常接受一个或多个参数/参数。它们不需要在那个特定的 CLI 界面中执行。为什么不直接在您已经在使用的 PowerShell CLI 界面中尝试呢?

标签: powershell cmd command-line


【解决方案1】:

$uninPaths 只是一个字符串。要像命令一样执行它,您可以将其提供给Invoke-Expression

Invoke-Expression -Command $uninPaths

Invoke-Command 一般用于在远程机器上运行 powershell 命令。

【讨论】:

  • 即使使用“Invoke-Expression”,它也只是输出字符串而不执行命令。