My main programming is done within Python, and want to invoke custom Powershell cmdlets I wrote. Added my .psm1
file to the $PSModulePath
, and my cmdlets are always loaded.
And I -NoProfile
, and -NoLogo
to invoke pwsh
cmd a little bit faster. Something like
cmd = ['pwsh', '-NoLogo', '-NoProfile', '-Command', cmdToRun]
process = Popen(cmd, stderr=PIPE, stdout=PIPE)
But this is still taking 5+ secs to return/process.
Does anyone know if there are other enhancements to run powershell scripts even faster? TIA
Python cannot host PowerShell in-process, so you cannot avoid the costly creation of a PowerShell child process, via pwsh
, the PowerShell (Core) 7 CLI (the same applies analogously to powershell.exe
, the Windows PowerShell CLI).
-NoProfile
would only make a difference if large / slowly executing $PROFILE
file(s) were present, and -NoLogo
is redundant, as it is implied by -Command
.
If you need to make PowerShell calls repeatedly in a given invocation of your application, you can mitigate the performance impact by launching a PowerShell CLI process in REPL mode, by passing -Command -
and then feeding commands to it on demand via stdin, terminating each with two newlines (to ensure that the end of the command is recognized), similar to the approach in this post.
You would then incur the penalty of the PowerShell child-process creation only once per run of your application, before submitting the first PowerShell command.