How to Call PowerShell Commands from Python and Pass Output to Python

Asked 1 years ago, Updated 1 years ago, 290 views

I tried calling the PowerShell command from Python as follows, but the return value was only displayed on the screen, but I could not receive it from Python.

importos
os.system("powerershell-Command Get-ItemProperty HKLM:SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*^|Select-Object DisplayName^|Format-Table –AutoSize")

How can I pass the data to Python after outputting the PowerShell output to a text file?

python windows powershell

2023-01-03 01:52

1 Answers

In addition to the comments, the following article is simply described:
Run Windows commands in Python's subprocess
Call Commands from Python (Windows Edition)

If you apply it, you can do it as follows.

import subprocess
res=subprocess.run("powershell-Command Get-ItemProperty HKLM:SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*^|Select-Object DisplayName^|Format-Table–AutoSize", 
    stdout=subprocess.PIPE, shell=True, encoding="cp932")
print(res.stdout)


2023-01-03 04:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.