2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PowerShellのスクリプト実行ポリシーエラーをVSCode側だけで解決する方法

2
Posted at

VSCode 「このシステムではスクリプトの実行が無効になっているため~」 エラーの解決方法

WindowsのPowerShellにはスクリプト実行ポリシーという安全機能があります。
既定値は Restricted で、PowerShellスクリプトの実行をブロックします。
今回はVSCodeのターミナル上で引っかかっちゃって、その環境でだけ解消させたので書き残します。

PS C:\Users\<YourUserName>\path\to\project> .\.venv\Scripts\Activate.ps1
.\.venv\Scripts\Activate.ps1 : このシステムではスクリプトの実行が無効になっているため、
ファイル C:\Users\<YourUserName>\path\to\project\.venv\Scripts\Activate.ps1 を読み込むことができません。
詳細については、「about_Execution_Policies」
(https://go.microsoft.com/fwlink/?LinkID=135170) を参照してください。
発生場所 行:1 文字:1
+ .\.venv\Scripts\Activate.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : セキュリティ エラー: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

手順

  1. VSCodeの設定ファイルを開く

    方法A(出る場合)

    • Ctrl + Shift + P → 「Preferences: Open Settings (JSON)」 を選択
      (または ファイル > ユーザー設定 > 設定 → 右上の {} アイコン)

    方法B(Preferences: Open Settings (JSON) が表示されない場合)

    1. メニューの「表示」→「コマンド パレット」を開く
    2. 「settings」と入力し、「Preferences: Open Settings (JSON)」 を選択
    3. 設定画面右上または検索結果内に出る 「Edit in settings.json」 というリンクをクリック(ちょっと探してみて、いくつかあります)

image.png

  1. JSONの中にこの設定を追加(他の設定がある場合はカンマに注意)

    "terminal.integrated.env.windows": {
        "PSExecutionPolicyPreference": "RemoteSigned"
    }
    

このとき、はじめてsetting.jsonを修正した人は画像と同じ状態なら問題ないです。
image.png

  1. 保存して VSCodeを再読み込み

    • Ctrl + Shift + P → 「Reload Window」
    • またはVSCodeを再起動
  2. VSCodeのターミナル(PowerShell)で試す

    .\.venv\Scripts\Activate.ps1
    

    → エラーが出ず、プロンプトの先頭に (venv) が付けば成功


💡 これだとVSCodeで開くPowerShellだけRemoteSigned 扱いになります。
外部のPowerShellやcmdには影響しないので用途に応じて実施してみてください

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?