LoginSignup
0
2

超簡単に.Ps1を実行する方法

Last updated at Posted at 2022-11-13

バッチからPowershellスクリプト(.ps1)を実行させる方法
とりあえずbatとps1のファイル名そろえておけば脳死で実行できるので重宝してます

フォルダ構成

フォルダ構成はこんな感じ。
※.batと.ps1ファイル名は揃えること
※.batは文字コードANSIで保存すること
image.png

batの中身

batの中身はこんな感じ。ファイル名変えれば中身弄らないで使いまわせます

aaa.bat
@echo off
powershell -executionpolicy RemoteSigned -File "%~dp0%~n0.ps1"
コマンド 意味
powershell Powershellを起動します
-executionpolicy RemoteSigned 実行ポリシーにRemoteSigned(適度に緩めのポリシー)を指定します。実行ポリシーについてはこちら
-File ファイルを指定します。
%~dp0%~n0.ps1 %~dp0 ⇒ バッチがあるフォルダパスの変数
%~n0 ⇒ バッチファイル名の変数
.ps1 ⇒ ".ps1"という文字列
↑を総括すると、バッチと同じフォルダ\バッチと同じファイル名.ps1
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2