Hello there,
The below thread might help in building your script.
https://devblogs.microsoft.com/scripting/use-a-powershell-module-to-run-windows-update/
Similar discussion here
Hope this resolves your Query !!
--If the reply is helpful, please Upvote and Accept it as an answer--