Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

PowerShell 執行原則受限無法執行腳本?一鍵解除限制

編輯:系統常見問題解答

初次使用 PowerShell 執行 .ps1 腳本時,預設會遇到錯誤:
因為在此系統中禁止執行腳本...
原因是預設執行原則為 Restricted。只需兩行指令即可解除限制 (需以系統管理員身分執行 PowerShell)。

解決步驟

PS C:\> Get-ExecutionPolicy
Restricted

PS C:\> Set-ExecutionPolicy RemoteSigned
# 出現確認提示輸入 Y 或 A

PS C:\> Get-ExecutionPolicy
RemoteSigned

執行原則說明

原則說明
Restricted預設值,禁止執行任何腳本,僅允許互動式指令
RemoteSigned推薦:本機腳本可直接執行,從網路下載的腳本需有受信任簽章
AllSigned所有腳本 (含本機) 均需簽章
Unrestricted完全開放,不檢查簽章 (安全性風險高)
Bypass不阻擋任何執行,無警告 (僅供特定自動化場景)

範圍控制 (進階)

# 僅對目前使用者生效 (無需管理員權限)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

# 對本機所有使用者生效 (需管理員)
Set-ExecutionPolicy RemoteSigned -Scope LocalMachine

建議開發機使用 RemoteSigned -Scope CurrentUser,生產/伺服器環境維持 Restricted 或配合簽章機制使用 AllSigned。

Copyright © Windows教程網 All Rights Reserved