![見出し画像](https://assets.st-note.com/production/uploads/images/141487303/rectangle_large_type_2_9e10f5dae57e89c198a91f4675e9c421.png?width=800)
PythonからPowerShellやBatファイルを実行する方法
PythonからPowerShellを実行したいとき、ありますよね。
逆は簡単なんですけどPythonからPowerShellを実行する方法が分からない
そんなときは、↓↓↓↓こうです。
# OSを扱うライブラリインポート
import os
# サンプル1
os.system('powershell -Command Get-Date')
# サンプル2 ポップアップも出せます
os.system("powershell -Command $wsobj = new-object -comobject wscript.shell;$wsobj.popup('こんにちわコルタナです')")
# PS1ファイルも実行できる 注意するのはスラッシュが「\\」なこと
os.system('powershell -Command "powershell -ExecutionPolicy RemoteSigned c:\\temp\\test.ps1"')
# batファイルも実行できる
os.system('powershell -Command "c:\\temp\\test.bat"')
実行ポリシーも回避できるから便利。
セキュリティ的にはちょっと怖いかも
#Python #PowerShell #コマンドレット #プログラミング学習 #プログラミング初心者 #Windows
この記事が気に入ったらサポートをしてみませんか?