﻿# 如何修復 PowerShell 無法執行任何 Cmdlet 的問題

在我目前這台 Windows 10 電腦中，有兩個 Windows PowerShell 捷徑，分別是 **Windows PowerShell** 與 **Windows PowerShell (x86)** 這兩個，從捷徑名稱很容易看得出其差異，**Windows PowerShell** 這個是在 64 位元環境下使用的 PowerShell，可以載入 64 位元的各式 PSModules 或 PSSnapin 到執行環境中。而 **Windows PowerShell (x86)** 則是在 32 位元的 x86 架構下使用的 PowerShell，可以載入那些只支援 x86 架構的 PSModules 或 PSSnapin 到執行環境中。不過今天在我的電腦，又再次發生 **Windows PowerShell (x86)** 視窗開啟後，任何 Cmdlet 都無法執行的窘境，這個問題我在 4 個月前曾經遇過一次，當時花了我一個多小時才找到解法。但今天又花了我將近一個小時找出問題並修復他，真是太浪費時間了，因此我決心寫一篇文章，記錄這個詭異的靈異現象與更詭異的解決方法 (至今尚未能理解這個問題發生的原因)。

我們先來看看執行 **Windows PowerShell (x86)** 之後無法執行任何 Cmdlet 命令的畫面：

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/745baa97088c596141d2-image_2777.png)

基本上，所有 PowerShell 內建的 Cmdlet 全部都消失了，完全無法執行。

每次遇到這種非常非常基本執行的問題，最困難的地方就是如何到 Google 輸入正確的關鍵字，經常就是一直查都查不到相關解答，因為這個問題太基礎了，連 dir 或 cd 這麼簡單的 alias 都無法執行，真的很難描述問題。

後來我用 **Get-Module -All** 搜尋所有的模組，發現在 **Windows PowerShell (x86)** 底下只有兩個模組可用：

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/603504c31073d58f55da-image_2778.png)

而在 **Windows PowerShell** 底下卻有更多模組可用，原本預設該載入的內建模組都有正常載入：

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/76505a2e871ce97298c2-image_2779.png)

**發生問題的原因不明，尚待釐清！  
****目前只知道我所使用的好幾台電腦中，出現這個問題的機率非常低，上次遇到相同問題是在 4 個月前，今天遇到第 2 次，只是每次都花我不少時間解決，覺得很煩。**

照理說，**Windows PowerShell** 會預設會從 **C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules** 目錄下載入模組，而其他的模組會參考 **$env:PSModulePath** 環境變數的定義，依序載入個別目錄下的模組。如果你想得知目前你的環境變數下有多少預設載入的模組路徑，可以輸入以下命令：**$env:PSModulePath -split ';'**

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/c4cd0c82d434b1bd52e0-image_2780.png)

### 解決方案

只要在無法執行任意 Cmdlet 的視窗中，輸入以下命令，問題就會自動修復：

**$env:PSModulePath = $env:PSModulePath + ";C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\"**

其實上述這段命令，只是將目前執行環境下的 PSModulePath 額外加上一個路徑而已，照理說重開 **Windows PowerShell (x86)** 視窗後，問題應該就會存在才對。但是，問題就真的這樣消失了，這個 **$env:PSModulePath** 環境變數依然沒有定義 **C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\** 這個路徑，但是…

問題消失了！

問題消失了！

問題消失了！

為什麼啊啊啊啊啊啊啊啊？ (抓頭)

如果你照著上述步驟還是無法修復問題，建議你直接去修改系統環境變數 **PSModulePath** 這一個，並且把 **C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\** 這個路徑加入到清單中，問題應該也可以順利解決。

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/34efc3c1ff19e413a074-image_2781.png)

**相關連結**

-   [windows server 2008 r2 - How do I fix a broken Powershell installation? - Stack Overflow](http://stackoverflow.com/questions/17654612/how-do-i-fix-a-broken-powershell-installation)
-   [Windows PowerShell can not find modules - Server Fault](http://serverfault.com/questions/531101/windows-powershell-can-not-find-modules)
-   [PowerShell Basics - Extending the Shell with Modules and Snapins](http://www.darkoperator.com/blog/2013/2/19/powershell-basics-extending-the-shell-with-modules-and-snapi.html)
-   [Get-Module](https://technet.microsoft.com/zh-TW/library/dd819449.aspx)
-   Plurk - 2015-8-16 10:16PM - [Will 保哥 說 剛被 PowerShell 給雷到，所有內建指令都無法用，不知道是哪個王八蛋安裝程式把我改壞的！ X-(](http://www.plurk.com/p/l5xcam)
-   [Windows PowerShell: Sharing Your Scripts - Made Easy | TechNet Magazine](https://technet.microsoft.com/en-us/magazine/ff625835.aspx)
-   [The Will Will Web | 如何在 Powershell 開啟時自動引入常用的 ps1 指令檔腳本](/post/2013/01/02/How-to-auto-load-ps1-script-using-Windows-PowerShell-Profiles.aspx)
