![](https://batch-file.net/wp-content/themes/cocoon-master/images/b-woman.png)
バッチファイルから
・PowerShellファイルを
・同期実行する
方法を記載します!
![](https://batch-file.net/wp-content/themes/cocoon-master/images/b-woman.png)
PowerShellファイルの実行結果も
取得できます!
PowerShellのコード
ここでは例として
・「PowerShellからのメッセージ出力です!」というメッセージを出力する
・「test.ps1」を作成
します。
$ws = new-object -comobject wscript.shell
$result = $ws.popup("PowerShellからのメッセージ出力です!")
# 正常終了の場合、戻り値として「0」を返す
exit 0
# 異常終了の場合、戻り値として「1」を返す
# exit 1
![PowerShellファイル](https://batch-file.net/wp-content/uploads/2022/05/bat_exec_powershellfile_001.png)
バッチファイルのコード
ここでは例として
・上記で作成したPowerShellのファイル「test.ps1」を
・同期実行
します。
※ここでは先程作成したPowerShellのファイル「test.ps1」をデスクトップ配下に配置しています。
@echo off
rem PowerShellファイルのパスを指定
set psFile=C:\Users\user\Desktop\test.ps1
rem PowerShellファイルを同期実行
powershell -NoProfile -ExecutionPolicy Unrestricted "%psFile%"
rem 実行結果を確認
if %errorlevel% == 0 (
echo 正常終了しました。戻り値:%errorlevel%
) else (
echo 異常終了しました。戻り値:%errorlevel%
)
echo.
pause
exit
実行結果
PowerShellファイルを同期実行できました。
![実行結果①](https://batch-file.net/wp-content/uploads/2022/05/bat_exec_powershellfile_002.png)
![実行結果②](https://batch-file.net/wp-content/uploads/2022/05/bat_exec_powershellfile_003.png)
参考①
バッチファイルからバッチファイルを実行することもできます。
詳細は以下の記事をご確認ください。
参考②
バッチファイルからVBScriptファイルを実行することもできます。
詳細は以下の記事をご確認ください。
参考③
上記のコードで使用した以下の詳細は、公式サイトをご確認ください。
●「powershell」コマンド
●「if」と環境変数「errorlevel」