Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
3

超簡単に.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"
コマンド意味
powershellPowershellを起動します
-executionpolicy RemoteSigned実行ポリシーにRemoteSigned(適度に緩めのポリシー)を指定します。実行ポリシーについてはこちら
-Fileファイルを指定します。
%~dp0%~n0.ps1%~dp0 ⇒ バッチがあるフォルダパスの変数
%~n0 ⇒ バッチファイル名の変数
.ps1 ⇒ ".ps1"という文字列
↑を総括すると、バッチと同じフォルダ\バッチと同じファイル名.ps1
0
3
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
sakekasunuts

@sakekasunuts(sakekasu nuts)

お酒しゅき♡ 雑魚情シスです。普段はPowerShellでリソース管理したりSCCMでスクリプト配ったりしてます
Linked from these articles

Comments

No comments

Let's comment your feelings that are more than good

0
3

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address