r/PowerShell • u/bapesta786 • Feb 13 '26
Help with Windows (11) Updates for an Automated Build
I am working on an automated W11 24H2 image build using Packer, and while it does have a Windows update plugin, it doesn't work when building an image in Audit mode, therefore I am now exploring other methods of how to install Windows updates.
I have learned that I simply cannot just call a PS script that runs the PSWindowsUpdates commands, but instead I have to run via Invoke-WUJob.
So far I am able to automate the process of installing the module but when I attempt to run my Invoke-WUJob script, the scheduled task is created but just stays as Queued.
I am not sure what I am doing wrong. Any help would be much appreciated.
Here is my code:
Invoke-WUJob -ComputerName localhost -RunNow -Confirm:$false -Script {
Install-WindowsUpdate `
-MicrosoftUpdate `
-AcceptAll `
-ForceDownload `
-ForceInstall `
-IgnoreReboot `
-Category 'Security'
}
Get-ScheduledTask -TaskName "PSWindowsUpdate"
do {
$scheduledTask = Get-ScheduledTask -TaskName "PSWindowsUpdate"
Write-Host "PSWindowsUpdate task: $($scheduledTask.State)"
Start-Sleep -Seconds 10
} while ($scheduledTask.State -ne "Ready")
$taskExists = Get-ScheduledTask -TaskName "PSWindowsUpdate"
if ($taskExists) {
Get-ScheduledTask -TaskName "PSWindowsUpdate"
Unregister-ScheduledTask -TaskName "PSWindowsUpdate" -Confirm:$false
} else {
Write-Host "PSWindowsUpdate isn't listed as a Scheduled Task."
}
2
u/Modify- Feb 13 '26 edited Feb 13 '26
When I troubleshoot a script that does not run interactively I use:
Start-Transcript C:\temp\log.txt
<script> Stop-Transcript This usually tells me what's happening. You can also make your code more verbose to see where it hangs or produces errors. Bonus nugget: Get-Content C:\\temp\\log.txt -Wait\\ will continuously refresh the contents of the file in the shell, so you don’t have to open and close the file in Notepad every time you need an update to see where it's stuck. Good luck! Ps, on my phone so formatting is f'd..
1
u/BlackV Feb 13 '26
Have you tried
1
u/xCharg Feb 13 '26
Why bother with pswindowsupdate, you can install packages (including updates) using dism, either directly calling dism.exe or via powershell module which is a wrapper over dism.exe anyway.
4
u/BlackV Feb 13 '26
This does not solve your problem but please stop using back ticks like that
https://get-powershellblog.blogspot.com/2017/07/bye-bye-backtick-natural-line.html