共用方式為


第 1 章 - 開始使用 PowerShell

本章著重於尋找並啟動PowerShell,並解決新使用者使用PowerShell的初始痛點。 請遵循並逐步解說您實驗室環境計算機上的本章範例。

什麼是 PowerShell?

Windows PowerShell 是一種易於使用的命令行殼層和腳本環境,可自動化 Windows 系統的系統管理工作。 Windows PowerShell 已預安裝於所有新式版本的 Windows 作業系統上。

哪裡可以找到PowerShell

在 Windows 11 上尋找 PowerShell 最簡單的方式是輸入 PowerShell 搜尋列,如圖 1-1 所示。 請注意,Windows PowerShell 有四個不同的快捷方式。

圖 1-1 - 搜尋 PowerShell。

64 位版本的 Windows PowerShell 快捷方式:

  • Windows PowerShell
  • Windows PowerShell ISE
  • Windows PowerShell (x86)
  • Windows PowerShell ISE (x86)

在 64 位版本的 Windows 上,您有 64 位版本的 Windows PowerShell 控制台和 Windows PowerShell 整合式腳本環境 (ISE) 和每一個 32 位版本,如快捷方式上的 (x86) 後綴所指示。

注意

Windows 11 僅隨附為 64 位作業系統。 沒有 32 位版本的 Windows 11。 不過,Windows 11 包含 32 位版本的 Windows PowerShell 和 Windows PowerShell ISE。

如果您執行舊版的 32 位 Windows,則只有兩個快捷方式。 這些快捷方式沒有 (x86) 後綴,但為32位版本。

如果您執行 64 位作業系統,除非您有使用 32 位版本的特定原因,否則建議您使用 64 位版本的 Windows PowerShell。

視您執行的 Windows 11 版本而定,Windows PowerShell 可能會在 Windows 終端機開啟。

Microsoft不再更新 PowerShell ISE。 ISE 僅適用於 Windows PowerShell 5.1。 Visual Studio Code (VS Code ) 與 PowerShell 擴充功能 搭配使用這兩個版本的 PowerShell。 VS Code 和 PowerShell 擴充功能不會隨附於 Windows 中。 在建立PowerShell腳本的電腦上安裝 VS Code 和擴充功能。 您不需要在執行 PowerShell 的所有電腦上安裝它們。

如何啟動PowerShell

我在支持的生產環境中使用三個不同的 Active Directory 用戶帳戶。 我鏡像了這本書中使用的實驗室環境中那些帳戶。 我以網域使用者身分登入 Windows 11 計算機,而沒有網域或本機系統管理員許可權。

按兩下 Windows PowerShell 快捷方式以 啟動 PowerShell 控制台,如圖 1-1 所示。 請注意,主控台的標題欄會顯示 Windows PowerShell,如圖 1-2 所示。

圖 1-2 - PowerShell 視窗的標題列。

當您以一般使用者身分執行PowerShell時,某些命令會正常執行。 不過,PowerShell 不會參與使用者 存取控制 (UAC) 。 這表示無法針對需要系統管理員核准的工作提示提高許可權。

注意

UAC 是 Windows 安全性功能,可協助防止惡意代碼以提升的許可權執行。

以一般使用者身分登入時,當您執行需要提高許可權的命令時,PowerShell 會傳回錯誤。 例如,停止 Windows 服務:

Stop-Service -Name W32Time
Stop-Service : Service 'Windows Time (W32Time)' cannot be stopped due to
the following error: Cannot open W32Time service on computer '.'.
At line:1 char:1
+ Stop-Service -Name W32Time
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (System.ServiceProcess.ServiceCon
   troller:ServiceController) [Stop-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Comm
   ands.StopServiceCommand

解決方案是以本機系統管理員身分提升許可權的使用者身分執行 PowerShell。 這就是我設定第二個網域用戶帳戶的方式。 遵循最低許可權原則,此帳戶不應該是網域系統管理員,或擁有網域中任何提高的許可權。

若要以提升的許可權啟動 PowerShell,請以滑鼠右鍵按兩下 Windows PowerShell 快捷方式,然後選取 [以系統管理員身分執行],如圖 1-3 所示。

圖 1-3 - 操作選單 - 以系統管理員身分執行。

Windows 會提示您輸入認證,因為您以一般使用者身分登入 Windows。 輸入網域使用者身為本機系統管理員的認證,如圖 1-4 所示。

圖 1-4 - 使用者帳戶控制 - 輸入認證。

請注意,提升許可權控制台視窗的標題欄會顯示 系統管理員:Windows PowerShell,如圖 1-5 所示。

圖 1-5 - 提升許可權 PowerShell 視窗的標題列。

既然您以系統管理員身分執行 PowerShell,當您執行需要提高許可權的命令時,UAC 就不再是問題。

重要

您只應該在必要時以系統管理員身分執行提升許可權的 PowerShell。

當您以遠端計算機為目標時,不需要執行提升許可權的 PowerShell。 提高許可權的執行 PowerShell 只會影響針對本機電腦執行的命令。

您可以簡化尋找和啟動 PowerShell。 將 PowerShell 或 Windows 終端機 快捷方式釘選到任務列。 再次搜尋 PowerShell,但這次以滑鼠右鍵按兩下 PowerShell,然後選取 [ 釘選到任務欄 ],如圖 1-6 所示。

圖 1-6 - 操作選單 - 釘選到任務列。

重要

這本書的原始版本於 2017 年出版,建議您在每次啟動 PowerShell 時,將快捷方式釘選到任務欄,以自動啟動提升許可權的實例。 不過,由於潛在的安全性考慮,我不再建議它。 您從提升許可權的 PowerShell 實例啟動的任何應用程式,也會略過 UAC 並執行提升許可權。 例如,如果您從已提升許可權的PowerShell實例啟動網頁瀏覽器,您造訪的任何網站也會提升許可權,其中包含惡意代碼。

當您需要以提升的許可權執行 PowerShell 時,以滑鼠右鍵按下釘選到任務列的 PowerShell 快捷方式,然後按 Shift 鍵。 選取 [ 以系統管理員身分執行],如圖 1-7 所示。

圖 1-7 - 操作功能表 - 以系統管理員身分執行。

判斷您的PowerShell版本

PowerShell 中有自動變數可儲存狀態資訊。 其中一個變數是 $PSVersionTable,其中包含PowerShell工作階段的版本資訊。

$PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.22621.2428
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.2428
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

如果您執行 5.1 之前的 Windows PowerShell 版本,您應該更新您的 Windows 版本。 Windows PowerShell 5.1 已預安裝於目前支援的 Windows 版本上。

PowerShell 7 版不是 Windows PowerShell 5.1 的取代專案;它會與 Windows PowerShell 並存安裝。 Windows PowerShell 5.1 版和 PowerShell 第 7 版是兩種不同的產品。 如需 Windows PowerShell 5.1 版與 PowerShell 第 7 版之間差異的詳細資訊,請參閱 從 Windows PowerShell 5.1 移轉至 PowerShell 7

提示

不再支援 PowerShell 第 6 版,先前稱為 PowerShell Core。

執行原則

PowerShell 執行原則會控制您可以執行 PowerShell 腳本的條件。 PowerShell 中的執行原則是一項安全功能,旨在協助防止意外執行惡意腳本。 不過,這不是安全性界限,因為它無法阻止判斷使用者刻意執行腳本。 決定的使用者可以在PowerShell中略過執行原則。

您可以設定本機電腦、目前使用者或 PowerShell 工作階段的執行原則。 您也可以使用組策略為使用者和計算機設定執行原則。

下表顯示目前 Windows 作業系統的預設執行原則。

Windows 作業系統版本 默認執行原則
Windows Server 2022 遠程簽署
Windows Server 2019 遠程簽署
Windows Server 2016 遠程簽署
Windows 11 受限制
Windows 10 受限制

不論執行原則設定為何,您都可以以互動方式執行任何 PowerShell 命令。 執行原則只會影響腳本中執行的命令。 Get-ExecutionPolicy使用 Cmdlet 來判斷目前的執行原則設定。

檢查電腦上的執行原則設定。

Get-ExecutionPolicy
Restricted

列出所有範圍的執行原則設定。

Get-ExecutionPolicy -List
        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine       Undefined

所有 Windows 用戶端作業系統都有的預設執行原則設定 Restricted。 您無法使用執行原則設定來執行 Restricted PowerShell 腳稿。 若要測試執行原則,請將下列程式代碼儲存為名為 Get-TimeService.ps1.ps1檔案。

提示

PowerShell 腳本是純文字檔案,其中包含您想要執行的命令。 PowerShell 腳本檔案會 .ps1 使用擴展名。 若要建立 PowerShell 腳本,請使用 Visual Studio Code (VS Code) 之類的程式碼編輯器或任何文字編輯器,例如記事本。

當您以互動方式執行下列命令時,它會完成而不會發生錯誤。

Get-Service -Name W32Time

不過,當您從腳本執行相同的命令時,PowerShell 會傳回錯誤。

.\Get-TimeService.ps1
.\Get-TimeService.ps1 : File C:\tmp\Get-TimeService.ps1 cannot be loaded
because running scripts is disabled on this system. For more information,
see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\Get-TimeService.ps1
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

當您在 PowerShell 中執行會產生錯誤的命令時,請先讀取錯誤訊息,再重試命令。 請注意,錯誤訊息會告訴您命令失敗的原因:

...在此系統上停用執行腳本。

若要啟用腳本的執行,請使用 Cmdlet 變更執行原則 Set-ExecutionPolicyLocalMachine當您未指定 Scope 參數時,會是預設範圍。 您必須以系統管理員身分執行PowerShell,才能變更本機電腦的執行原則。 除非您正在簽署文本,否則建議您使用 RemoteSigned 執行原則。 RemoteSigned 防止您執行未由信任發行者簽署的已下載腳本。

變更執行原則之前,請閱讀 about_Execution_Policies 說明文章以瞭解安全性影響。

將電腦上的執行原則設定變更為 RemoteSigned

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

如果您已成功變更執行原則,PowerShell 會顯示下列警告:

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust.
Changing the execution policy might expose you to the security risks
described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the
execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "N"):y

如果您未以系統管理員身分執行 PowerShell,PowerShell 會傳回下列錯誤訊息:

Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWAR
E\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To
change the execution policy for the default (LocalMachine) scope, start
Windows PowerShell with the "Run as administrator" option. To change the
execution policy for the current user, run "Set-ExecutionPolicy -Scope
CurrentUser".
At line:1 char:1
+ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy],
   UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.
   PowerShell.Commands.SetExecutionPolicyCommand

您也可以變更目前使用者的執行原則,而不需要您以系統管理員身分執行提升許可權的 PowerShell。 如果您成功將本機計算機的執行原則設定為 RemoteSigned,則不需要此步驟。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

當執行原則設定為 RemoteSigned時, Get-TimeService.ps1 腳本會順利執行。

.\Get-TimeService.ps1
Status   Name               DisplayName
------   ----               -----------
Running  W32Time            Windows Time

摘要

在本章中,您已瞭解如何尋找及如何啟動 PowerShell。 您也瞭解如何判斷 PowerShell 的版本和執行原則的目的。

檢閱

  1. 如何判斷電腦執行哪些 PowerShell 版本?
  2. 何時應以系統管理員身分啟動提升許可權的PowerShell?
  3. Windows 用戶端電腦上的預設執行原則為何,以及它如何防止您執行?
  4. 如何判斷目前的PowerShell執行原則設定?
  5. 如何變更 PowerShell 執行原則?

參考資料

若要深入瞭解本章所涵蓋的概念,請閱讀下列 PowerShell 說明文章。

下一步

在下一章中,您將瞭解 PowerShell 中命令的可探索性。 您也將瞭解如何下載PowerShell的說明檔,以便您可以在PowerShell工作階段中檢視說明。