Skip to main content

修改 PowerShell 指令碼執行策略

當 Windows 執行 Powershell 指令時,會出現以下錯誤,這時候我們可以通過設定執行原則來賦予執行其他程式碼的權限。

PS C:\Users\adm> npm i -g opencode-ai
npm : 因為這個系統上已停用指令碼執行,所以無法載入 C:\Program Files\nodejs\npm.ps1 檔案。如需詳細資訊,請參閱 about_Execution_Policies,網址為 https:/go.microsoft.com/fwlink/?LinkID=135170
。
位於 線路:1 字元:1
+ npm i -g opencode-ai
+ ~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
  1. 使用管理者權限啟動Powershell
  2. 輸入 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser ,將 Undefined更改為RemoteSigned,並將範圍預設值改為當前用戶
  3. 輸入 Get-ExecutionPolicy -List 可查詢目前的 Execution policy,範例如下。
   PS C:\Users\adm> Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine       Undefined
  • 更多關於 Execution policy 操作與說明請參考:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.4