如何通过 Python 打开 Windows Powershell



我已经知道如何通过python打开Windows命令提示符,但我想知道是否有办法打开Windows powershellx86窗口并通过Windows 3.7在Windows 10上运行命令?

你可以调用powershell.exe使用subprocess.run

import subprocess
subprocess.run('powershell.exe Get-Item *')

如果您知道如何运行命令提示符(CMD.EXE(,那么您应该能够使用相同的方法来运行PowerShell(PowerShell.EXE(。 默认情况下,PowerShell.EXE 位于 c:\windows\system32\windowspowershell\v1.0\ 中。 要使用命令运行外壳,请使用:

c:windowssystem32windowspowershellv1.0PowerShell.exe -c {commands}

若要启动 .ps1 脚本文件,请使用

c:windowssystem32windowspowershellv1.0PowerShell.exe -f PathScript.ps1

祝你好运。

最新更新