0

I am pulling in a text file that was previously created that simply lists the KB numbers for all currently installed Windows Updates. I am trying to pull this file in and call wusa.exe and pass it the KB number to perform an uninstallation.

$a = Get-Content c:\hotfixid.txt
foreach ($kb in $a) {
    $command = 'cmd /c wusa.exe /uninstall /kb:' + $kb.Trim().Substring(2) + ' /quiet /norestart'
    #Write-Host $command
    Write-Host "Currently uninstalling $kb"
    Invoke-Expression -Command:$command
}

If I use

Write-Host $command

and copy that directly to a run dialog box in Windows, it completes successfully.

What happens when I run it in a PowerShell script is that it only outputs the Write-Host portions one after the other in about 2 seconds. I do not see any command windows opening and I don't see it actually DOING anything. I am running the PowerShell script 'As Administrator' with an unrestricted execution policy. I have also tried adding 'runas' to the $command to call the CMD window each time with administrative privileges and it made no difference. Calling it via Invoke-Command as well makes no difference.

4
  • I think you are using the wrong cmdlet, try with start-processinstead of invoke-expression Commented Oct 30, 2014 at 15:16
  • I'll try that on my laptop where I'm running PowerShell v4.0 but the target computer is PowerShell v2.0. I'd likely have to upgrade the target computer if this ends up working. Would be nice to know how to get it to work in v2.0 though. Commented Oct 30, 2014 at 17:26
  • Well you know start-process is available in V2. If you want to keep your logic i think you can write your code in a batch file and use invoke-expression yourbatch.bat Commented Oct 30, 2014 at 17:52
  • Yeah, I had to look up what version 'Start-Process' was first supported in. I have modified the script and it is currently running. So the 'Start-Process' was the key, so thanks. Now, what the outcome is after running the script, that's another story ... Commented Oct 30, 2014 at 18:19

1 Answer 1

1

PowerShell can run most commands directly w/o too much trouble.

Using Invoke-Expression just complicates matters, as does Invoke-Command or Start-Process, because you need to get the quoting right, and pass the arguments in a slightly unnatural way.

You can even skip running cmd.exe most of the time.

Try the following:

wusa.exe /uninstall "/kb:$($kb.Trim().Substring(2))" /quiet /norestart
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.