整理這些技術筆記真的很花時間,如果你願意 關閉 Adblock 支持我,我會把這份感謝轉換成更多「踩坑轉避坑」的內容給你!ヽ(・∀・)ノ
解決 Windows 上輸入指令出現「因為這個系統上已停用指令碼執行,所以無法載入...」的問題
前言
身為一名工程師在 Terminal 上輸入指令是一件很常見的事情,但有些時候會出現「因為這個系統上已停用指令碼執行,所以無法載入…」的問題,所以這一篇就記錄一下該如何解決。
因為這個系統上已停用指令碼執行

1 | |
之所以會出現這個問題是因為 Windows 在執行政策上會有一定的安全性設置,其主要是避免你執行了一些不安全的指令來源,例如不小心執行了病毒指令等,因此在一般的狀況下 Windows 的執行政策 (Set-ExecutionPolicy) 預設是 Restricted,也就是「限制原則」。
而主要會發生這個問題通常都是在 Windows 10 的 PowerShell 上,官方似乎也沒有說明大概從哪一個版本開始 PowerShell 的執行原則就預設調整成 Restricted,因此若你是一名工程師的話,就會需要調整一下這個選項。
Set-ExecutionPolicy
在這邊先解釋一下 Set-ExecutionPolicy 相關指令設置說明,對於基本的概念若沒有認知的話是有很高的資安風險,以下這邊是依照官方破英文翻譯 + Google 翻譯。
AllSigned(僅限有簽名憑證)- Scripts can run. - 腳本可執行
- Requires that all scripts and configuration files be signed by a trusted publisher, including scripts that you write on the local computer. - 要求所有腳本和配置文件僅限受信任的發布者簽名憑證,包括您在本地計算機上所撰寫的腳本。
- Prompts you before running scripts from publishers that you haven’t yet classified as trusted or untrusted. - 在運行尚未歸類為受信任或不受信任的發布者的腳本之前提示您。
- Risks running signed, but malicious, scripts. - 運行帶有簽名但有惡意的腳本的風險。
- 翻譯蒟蒻:僅限有經過可信任的發行者簽署後才可以執行。
Bypass- Nothing is blocked and there are no warnings or prompts. - 不會有任何警示或提示。
- This execution policy is designed for configurations in which a PowerShell script is built in to a larger application or for configurations in which PowerShell is the foundation for a program that has its own security model. - 此執行策略設計用於將PowerShell腳本內置到更大的應用程序中的配置,或者用於以PowerShell為具有自己的安全模型的程序的基礎的配置。
- 翻譯蒟蒻: 你用不到。
Default- Sets the default execution policy. - 調整成預設原則
- Restricted for Windows clients. - 僅限Windows客戶端。
- RemoteSigned for Windows servers. - Windows服務器的RemoteSigned。
- 翻譯蒟蒻:就是預設模式。
RemoteSigned- The default execution policy for Windows server computers.
- Scripts can run.
- Requires a digital signature from a trusted publisher on scripts and configuration files that are downloaded from the internet which includes email and instant messaging programs.
- Doesn’t require digital signatures on scripts that are written on the local computer and not downloaded from the internet.
- Runs scripts that are downloaded from the internet and not signed, if the scripts are unblocked, such as by using the Unblock-File cmdlet.
- Risks running unsigned scripts from sources other than the internet and signed scripts that could be malicious.
- 翻譯蒟蒻:可以使用本機所撰寫的腳本檔案,不需要簽署就可以執行,但是從遠端下載的腳本就必須經由可信任的發行者簽署後才可以執行。
Restricted- The default execution policy for Windows client computers.
- Permits individual commands, but does not allow scripts.
- Prevents running of all script files, including formatting and configuration files (.ps1xml), module script files (.psm1), and PowerShell profiles (.ps1).
- 就算該指令來源以及憑證簽名是自可信用機構也一樣限制所有指令,簡單講就是我都不讓你用啦。
Undefined- There is no execution policy set in the current scope.
- If the execution policy in all scopes is Undefined, the effective execution policy is Restricted, which is the default execution policy.
- 翻譯蒟蒻:我懶得翻了,不重要的東西 XD
Unrestricted- The default execution policy for non-Windows computers and cannot be changed.
- Unsigned scripts can run. There is a risk of running malicious scripts.
- Warns the user before running scripts and configuration files that are not from the local intranet zone.
- 翻譯蒟蒻:這個指令的概念就是「前鬼,我還你原形」一樣,就是解除全部限制(曝露年齡)。
查看目前執行原則
準備解放功能之前可以先輸入以下指令查看目前執行原則
1 | |

另外小提一下可以加上參數 -List 查看更詳細一點。
1 | |

調整執行原則
接下來就是解決「因為這個系統上已停用指令碼執行,所以無法載入…」的問題,只需要在終端機輸入以下指令即可
1 | |
但是這邊要注意,請務必使用「系統管理員身份」執行上面指令,否則會出現權限不足

至於如何使用「系統管理員身份」很簡單,只需要針對終端機點右鍵「以系統管理員身份執行」即可

此時再輸入一次指令,就可以正常運行,但你應該會出現以下訊息

1 | |
此時你不用害怕,按下「A」就可以了,因為我們目前要更改的原則是 「RemoteSigned」,在前面有說過這個執行原則是「可以使用本機所撰寫的腳本檔案,不需要簽署就可以執行,但是從遠端下載的腳本就必須經由可信任的發行者簽署後才可以執行。」,因此安全性是較好的。
當你按下 「A」,你就可以再去試試看執行 npm 、vue 、 gulp 以及 webpack 等等指令囉~
記得終端機要全部關閉在開啟,這樣子效果才能夠正常使用以上指令唷。
參考文獻
整理這些技術筆記真的很花時間,如果你願意 關閉 Adblock 支持我,我會把這份感謝轉換成更多「踩坑轉避坑」的內容給你!ヽ(・∀・)ノ