Enjoy this Full Explanation demo.
import subprocess
result = subprocess.run("C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe Write-Output 'Hello world!' ", shell=True, capture_output=True) # command to run in powershell using python subprocess module
res = result.returncode # return system-exit code of the command
out = result.stdout # return output of the powershell command
print(f"The output of the command is {out}, The exit code is {res} and the process generated by command is {result}.")
output
The output of the command is b'Hello world!\r\n', The exit code is 0 and the process generated by command is CompletedProcess(args="C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe Write-Output 'Hello world!' ", returncode=0, stdout=b'Hello world!\r\n', stderr=b'').