了解 Windows PowerShell

已完成

PowerShell 是一個自動化解決方案,由命令列殼層、指令碼語言,以及組態管理架構所組成。

命令列殼層

Windows PowerShell 取代了 Windows 命令行介面(cmd.exe),以及其批處理文件腳本語言的有限功能。 PowerShell 能接受和傳回 .NET 物件,且包括:

  • 命令列歷程記錄。
  • Tab 自動完成和預測。
  • 支援命令和參數別名。
  • 使用管線功能的鏈結命令。
  • 完善的主控台內說明系統

一開始,Windows PowerShell 是以 .NET Framework 為基礎而建置的平台,而且只適用於 Windows 作業系統。 不過最新版本的 PowerShell 採用 .NET Core,而且可以在 Windows、macOS 和 Linux 平台上執行。 由於可以支援多個平台,因此這些最新版本改稱為 PowerShell (而不是 Windows PowerShell)。

指令碼語言

「命令」負責提供 PowerShell 的主要功能。 命令有許多品種,包括 Cmdlet(發音為 command-lets)、函式、篩選、腳本、應用程式、組態和工作流程。 命令是使用 Windows PowerShell 指令碼語言建立的建置組塊, 您可以使用命令來建立自訂解決方案,從而解決複雜的系統管理問題。 或者,您也可以直接在 PowerShell 主控台內執行命令以完成單一工作。 主控台是 PowerShell 的 CLI,也是您與 PowerShell 互動的主要方式。

Cmdlet 會使用「動詞-名詞 (Verb-Noun)」命名慣例。 例如,您可以使用 Get-Command Cmdlet 來列出命令殼層中註冊的所有 Cmdlet 和函式。 動詞會識別要執行的 Cmdlet 動作,而名詞會識別 Cmdlet 要執行動作的資源。

Microsoft 伺服器應用程式和雲端服務會提供特殊的 Cmdlet,讓您用來管理這些服務。 事實上,某些功能「只能」使用 PowerShell 進行管理。 在許多情況下,即使應用程式提供圖形化使用者介面 (GUI) 來管理特定功能,此介面仍依靠 PowerShell 在幕後實作至少其中一部分的功能。

組態管理架構

PowerShell 會納入 PowerShell Desired State Configuration (DSC) 管理架構。 此架構可讓您使用程式碼來管理企業基礎結構,以協助:

  • 針對可重複的部署使用宣告式設定和可重複的指令碼。
  • 強制執行組態設定,並識別設定偏離標準需求的時間。
  • 使用推送或提取模型來部署組態設定。

採用以 PowerShell 為基礎管理功能的應用程式和服務運作方式一致。 這表示您可以快速應用您學到的課程。 此外,當您使用自動化指令碼來管理軟體應用程式時,您可以在其他應用程式中重複使用這些指令碼。

Windows PowerShell 版本

在學習 PowerShell 時,請務必您的作業系統 (OS) 類型和版本,了解可能會遇到的各種版本。 主要的 PowerShell 平台有兩個:

  • Windows PowerShell
  • PowerShell (原本稱為 PowerShell Core)

Windows PowerShell

Windows PowerShell 僅適用於 Windows OS。 Windows PowerShell 1.0 於 2006 年推出,做為 Windows XP Service Pack 2 (SP2)、Windows Server 2003 SP1 和 Windows Vista 上可安裝的元件。 另外也是 Windows Server 2008 的選用元件。 在 2009 年,PowerShell 2.0 已整合到 Windows 7 和 Windows Server 2008 R2 中。 所有版本的 Windows PowerShell (至 5.1 版本,適用於 Windows 10) 都會與 Windows OS 整合。

Windows PowerShell 是作業系統元件,因此會接收與其父作業系統相同的生命週期支援和授權合約。

PowerShell

PowerShell 的提供、安裝及設定與 Windows PowerShell 各自獨立。 PowerShell Core 6.0 首次發行於 2018 年,這是提供多平台支援的第一個版本,擴充支援 macOS 和 Linux 作業系統。

注意

最新版本的PowerShell是PowerShell 7.5,可透過 Microsoft Update 取得。

PowerShell 和 Windows PowerShell 的安裝各自獨立,而且您可以使用任一環境執行支援的命令。

標準 Windows 授權合約不包含 PowerShell。 PowerShell 的支援是由傳統 Microsoft 付費支援、Microsoft Enterprise 合約與 Microsoft 軟體保證所提供。 您也可取得論壇支援。

版本發行歷程記錄

下表提供主要 PowerShell 版本的一般時間軸:

表 1:PowerShell 發行時程表

版本 發行日期 備註
PowerShell 7.5 2025年1月 建置在 .NET 9.0 上。
PowerShell 7.4 2023 年 11 月 建置在 .NET 8.0 上。
PowerShell 7.3 2022 年 11 月 建置在 .NET 7.0 上。
PowerShell 7.2 2021 年 11 月 基於 .NET 6.0 建置。
PowerShell 7.1 2020 年 11 月 基於 .NET 5.0 建置。
PowerShell 7.0 2020 年 3 月 基於 .NET Core 3.1 建置。
PowerShell 6.0 2018 年 9 月 基於 .NET Core 2.0 建置。 第一個可在 Windows、Linux 和 macOS 上安裝的版本。
PowerShell 5.1 2016 年 8 月 在 Windows 10 年度更新版和 Windows Server 2016 中發行,並包含在 Windows Management Framework (WMF) 5.1 中。
PowerShell 5.0 2016 年 2 月 整合至 Windows 10 1511 版。 在 Windows Management Framework (WMF) 5.0 中發行。 可在 Windows Server 2008 R2、Windows Server 2012、Windows 10、Windows 8.1 企業版、Windows 8.1 專業版和 Windows 7 SP1 上安裝。
PowerShell 4.0 2013 年 10 月 整合至 Windows 8.1 和 Windows Server 2012 R2 中。 可在 Windows 7 SP1、Windows Server 2008 SP1 和 Windows Server 2012 上安裝。
PowerShell 3.0 2012 年 10 月 整合至 Windows 8 和 Windows Server 2012 中。 可在 Windows 7 SP1、Windows Server 2008 SP1 和 Windows Server 2008 R2 SP1 上安裝。
PowerShell 2.0 2009 年 7 月 整合至 Windows 7 和 Windows Server 2008 R2 中。 可在 Windows XP SP3、Windows Server 2003 SP2 與 Windows Vista SP1 上安裝。
PowerShell 1.0 2006 年 11 月 可在 Windows XP SP2、Windows Server 2003 SP1 與 Windows Vista 上安裝。 Windows Server 2008 的選用元件。

注意

在此課程模組中,主題將同時與最新的 Windows PowerShell 和 PowerShell 版本 (5.1 和 7.5) 相關。 大部分 Cmdlet 都適用於兩個平台。 不過,如果僅支援特定功能或與某一個特定平台相關,則會有附註。

其他閱讀: 若要在較低版本的作業系統上支援較新版本的 PowerShell,您可能需要安裝最新版的 Windows Management Framework。 如需詳細資訊,請參閱 安裝和設定WMF 5.1