1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

batファイルから一時的に制限を解除してPowerShellスクリプトファイルを呼び出す

Posted at

PowerShellスクリプトファイル(ps1ファイル)を実行する場合、実行ポリシーによる制限を解除する必要があります。

実行ポリシーの詳しい内容は以下サイトが大変参考になります。
PowerShellの「実行ポリシー」とは何か?

今回はサンプルとして、batファイルで指定して引数に対して、特定の文字列を追加したメッセージを表示するPowerShellスクリプトを用意しました。

Hello.ps1
$input = $args[0]

$message = "ようこそ " + $input

write-host $message
sample.bat
@echo off
setlocal

powershell -ExecutionPolicy Bypass -command .\Hello.ps1 "田中さん"

endlocal
pause
結果
ようこそ 田中さん

実行ポリシーはデフォルトで「Restricted(全てのpsスクリプトの実行を禁止する)」になっていますが、
上記コマンドで実行ポリシーを「Bypass(全てのpsスクリプトの実行が可能)」に変更しています。
処理が完了すると、実行ポリシーは「Restricted」に戻ります。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?