How can I execute scripts in a code created powershell shell that has Write-Host commands in it?
The fastest way to get this to work is to define a dummy write-host
function in your script, or to simply define it in the runspace independently before running your script.
$ps.addscript("function write-host {}").invoke()
$ps.commands.clear()
# now you can invoke scripts that use write-host
# feel free to implement a write-host that writes to a log file
Simple as that. The reason you're getting that error is because programmatic invocation like that does not expect user interaction. There are ways to make this work but it employs different APIs.
If you need the output, you can use:
$ps.addscript("function write-host($out) {write-output $out}").invoke()
$ps.commands.clear()