Ⅰ. はじめに
タイトルの通り「PowerShell スクリプトを実行する方法」です。
セキュリティ上、デフォルト設定ではPowerShell スクリプトを実行する事が出来ません。
以下のエラーが出力されます。
PS C:\> .\test.ps1
.\test.ps1 : このシステムではスクリプトの実行が無効になっているため、ファイル C:\test.ps1 を読み込
むことができません。詳細については、「about_Execution_Policies」(https://go.microsoft.com/fwlink/?LinkID=135170) を参照
してください。
発生場所 行:1 文字:1
+ .\test.ps1
+ ~~~~~~~~~~
+ CategoryInfo : セキュリティ エラー: (: ) []、PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
Ⅱ. やり方
1. 以下のコマンドを実行する
Set-ExecutionPolicy Bypass -Scope CurrentUser
FAQ
Q. ps1 ファイルをダブルクリックで実行したい
A. 以下のテキストをx.regという名前をつけて保存し、実行する。
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\shell\open\command]
@="\"C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe\" \"%1\""
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\ShellEx\DropHandler]
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"