PowerShell スクリプトの実行方法
-
PowerShell で
./script_name
を使用して PowerShell スクリプトを実行する - PowerShell で完全なパスを使用して PowerShell スクリプトを実行する
-
cmd.exe
を使用して PowerShell スクリプトを実行する -
cmd.exe
で PowerShell スクリプトを実行するために-File
パラメータを使用する -
cmd.exe
で PowerShell スクリプトを実行するためにbypass
スイッチを使用する -
cmd.exe
で PowerShell スクリプトを実行するためにtype
コマンドを使用する

PowerShell スクリプトは、.ps1
拡張子のファイルに保存されたコマンドのコレクションです。PowerShell は、.ps1
ファイルに書かれたコマンドを実行します。
私たちは myscript.ps1
という名前の PowerShell スクリプトを作成しました。その中には以下のコマンドが含まれています。
Write-Host "Your script is executed successfully."
出力:
Your script is executed successfully.
上記の出力は、myscript.ps1
を実行したときに表示されるべきです。このチュートリアルでは、PowerShell スクリプトを実行するためのさまざまな方法を紹介します。
PowerShell で ./script_name
を使用して PowerShell スクリプトを実行する
この方法を使用するには、スクリプトファイルがあるディレクトリにいる必要があります。cd
コマンドは、PowerShell で作業ディレクトリを変更するために使用されます。スクリプトファイルのディレクトリに移動した後、./script_name
を実行します。
例えば、私たちのスクリプトファイルは C:\New
にあります。
cd C:\New
その後、スクリプトを実行します。
./myscript.ps1
出力:
Your script is executed successfully.
PowerShell で完全なパスを使用して PowerShell スクリプトを実行する
この方法では作業ディレクトリを変更する必要はありません。スクリプトファイルの完全なパスを指定して実行できます。
C:\New\myscript.ps1
出力:
Your script is executed successfully.
cmd.exe
を使用して PowerShell スクリプトを実行する
コマンドプロンプトから PowerShell スクリプトを実行できます。-noexit
引数は必須ではありません。スクリプトが終了した後に PowerShell が終了するため、コンソールを開いたままにします。
powershell -noexit C:\New\myscript.ps1
出力:
Your script is executed successfully.
cmd.exe
で PowerShell スクリプトを実行するために -File
パラメータを使用する
-File
パラメータを使用すると、cmd.exe
などの別の環境からスクリプトを呼び出すことができます。
powershell -File C:\New\myscript.ps1
出力:
Your script is executed successfully.
cmd.exe
で PowerShell スクリプトを実行するために bypass
スイッチを使用する
デフォルトのスクリプト実行ポリシーを変更せずに PowerShell スクリプトを実行するために、bypass スイッチを使用できます。
powershell -executionpolicy bypass -File C:\New\myscript.ps1
出力:
Your script is executed successfully.
cmd.exe
で PowerShell スクリプトを実行するために type
コマンドを使用する
cmd
で PowerShell スクリプトを実行するために、type
コマンドを使用することもできます。
type "C:\New\myscript.ps1" | powershell -c -
出力:
Your script is executed successfully.