了解 Windows PowerShell
PowerShell 是一种自动化解决方案,由命令行 shell、脚本语言和配置管理框架组成。
命令行 Shell
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-let)、函数、筛选器、脚本、应用程序、配置和工作流。 命令是使用 Windows PowerShell 脚本语言组合在一起的构建基块。 使用命令可创建自定义解决方案来解决复杂的管理问题。 或者,可直接在 PowerShell 控制台中运行命令来完成单个任务。 此控制台为适用于 PowerShell 的 CLI,是与 PowerShell 交互的主要方式。
cmdlet 使用“谓词-名词”命名约定。 例如,可使用 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 是一个 OS 组件,因此它获得与其父操作系统相同的生命周期支持和许可协议。
PowerShell
PowerShell 与 Windows PowerShell 分开提供、安装和配置。 2018 年首次发布的 PowerShell Core 6.0,是第一个提供多平台支持的版本,将其可用性扩展到 macOS 和 Linux 操作系统。
注意
最新版本的 PowerShell 是 PowerShell 7.5,可通过 Microsoft Update 获取。
PowerShell 和 Windows PowerShell 分开安装,你可以使用任一环境运行受支持的命令。
标准 Windows 许可协议不包括 PowerShell。 而是享有 Microsoft 付费支持、Microsoft 企业协议和 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。