Netwrix 1Secure 提供跨数据和身份的统一可见性——免费试用14天,享有完全访问权限。开始免费试用

资源中心博客

下载并安装 PowerShell 7

下载并安装 PowerShell 7

Aug 25, 2025

PowerShell 7 是基于 .NET Core 构建的跨平台自动化框架,提供更高性能、并行处理能力,并与 Windows PowerShell 模块具有广泛兼容性。它可在 Windows、macOS、Linux 和容器中运行,并提供灵活的安装选项,例如使用软件包管理器、MSI、ZIP 或 Docker。长期支持(Long-Term Support)版本为企业提供稳定性,而预览构建则让用户能够更早体验新功能。

PowerShell 7 简介

PowerShell 7 是一款基于 Windows PowerShell 功能构建的现代跨平台(Windows、macOS 和 Linux)自动化与配置管理工具。它以 .NET Core 运行时为基础,而不像先前版本的 PowerShell 那样依赖仅限 Windows 的 .NET Framework。这样的转变使得在兼容性、性能和灵活性方面获得了显著提升。

PowerShell 7 的其他功能与增强包括:

  • PowerShell 7 引入了 ForEach-Object -Parallel,,使用户能够在并行管道中处理多个项目。这将显著降低可并行化工作负载的执行时间。
  • 新增的 cmdlet 和 API(包括对 Get-Help 等命令的更新)使脚本环境更丰富、也更灵活多样。
  • 新的 -ErrorAction-ErrorVariable 选项可以更好地控制脚本中的错误处理。
  • PowerShell 7 包含一个 Windows 兼容性层,使大多数为 Windows PowerShell 构建的模块(例如 Active Directory 或 Exchange 模块)能够在基于 Windows 的环境中通过隐式远程(implicit remoting)运行。
  • PowerShell 7 可以与 Windows PowerShell 一起安装,从而在不打断现有工作流的情况下实现逐步迁移。它能够以最少的修改运行许多旧脚本。尽管并非所有较旧的模块都能原生工作,但兼容性层可确保覆盖范围相当可观。
  • PowerShell 7 通过 PowerShell 扩展与 Visual Studio Code 无缝集成,从而提供 IntelliSense、调试和集成测试等功能。
  • 最后,PowerShell 7 作为开源项目在 GitHub 上进行开发和维护。

如需了解 Windows PowerShell 和 PowerShell 7 之间的差异,请参阅 Check, Update, and Choose a PowerShell Version 文章。

Active Directory 安全最佳实践

了解更多

下载 PowerShell 7

在下载 PowerShell 7 之前,您必须了解可用的版本及其差异。

可用的 PowerShell 7 版本

Microsoft 为 PowerShell 7 提供三种类型的发布版本。

稳定版发布是指 PowerShell 的一般可用(General Availability)版本,通常被认为已可用于生产环境。这些版本在未被指定为 LTS(长期支持)之前,将获得 18 个月的支持。

稳定版发布

最新稳定版发布:PowerShell 7.4.6,于 2024 年 10 月 22 日发布。该版本基于 .NET 8.0,并被指定为长期支持(Long-Term Support,LTS)版本,支持期限延长至 2026 年 11 月。

LTS(长期支持)版本

与非 LTS 版本相比,LTS 版本将提供更长时间的支持(3 年)。这些版本面向企业以及对长期稳定性和兼容性要求至关重要的环境。

当前 LTS 版本:PowerShell 7.4,将支持至 2026 年 11 月

预览版本

预览版本可让您更早访问新功能和更新。这些版本旨在用于测试和反馈,但不建议用于生产环境。一旦预览版本定稿,它将成为稳定版本。

最新预览版本:PowerShell 7.5 Release Candidate 1(RC-1),于 2024 年 11 月 18 日发布。该版本基于 .NET 9 构建,并被视为“go-live”版本,意味着支持在生产环境中使用。PowerShell 7.5 将遵循 18 个月的标准支持生命周期。

版本时间线与支持摘要

Version

Release Type

Support Ends

.NET Version

Notes

7.4

LTS

November 2026

.NET 8

Current LTS version

7.5 RC-1

Preview

N/A (Preview)

.NET 9

Next stable candidate

7.2

LTS (Ended)

November 8, 2024

.NET 6

Previous LTS version

7.0

LTS (Ended)

February 2022

.NET Core 3.1

First LTS version

你应该使用哪个版本?

  • 在生产环境中需要长期稳定性时,选择 LTS。
  • 若要在保持生产就绪的同时访问新功能,请使用 Stable。
  • 使用 Explore Preview 测试即将推出的功能并提供反馈。

在哪里查找 PowerShell 7 的下载链接

您可以在 Microsoft 网站、官方 PowerShell GitHub 代码库以及其他经过验证的来源上找到 PowerShell 7 的下载链接。

Microsoft 网站

URL: 在 Windows、Linux 和 macOS 上安装 PowerShell

包含在以下平台上安装 PowerShell 的说明:

  • Windows:MSI 安装程序、Microsoft Store
  • macOS:.pkg 文件、Homebrew
  • Linux:.tar.gz 文件,以及 apt、yum 或 dnf 等原生软件包管理器

官方 PowerShell GitHub 发布页面

URL:PowerShell 发布

GitHub 发行页面提供以下内容的下载链接:

  • 稳定版本
  • 长期支持版本(LTS)
  • 预览版

选择所需版本,并为您的操作系统下载安装程序包(Windows、macOS、Linux)。

直接使用包管理器

  • Windows – 通过 Microsoft Store 安装以启用自动更新
  • macOS – 通过 Homebrew 安装
  • Linux – 使用发行版的包管理器(例如:apt、yum、dnf、zypper)

Winget 包管理器

要使用 Windows 包管理器(winget)在 Windows 系统上安装 PowerShell 的最新版本,请使用以下命令:

      winget install --id Microsoft.PowerShell --source winget
      

Docker 镜像

URL:Docker Hub

PowerShell 7 提供预构建的 Docker 镜像。您可以拉取最新的 LTS 或稳定版发布。

PowerShell 7 的安装选项

PowerShell 7 可使用针对各平台定制的多种方法安装到 Windows、macOS 和 Linux 上。

Windows 上的安装

  • 如果您希望安装过程简单,Microsoft Store 是最佳选择。此外,它还提供自动更新。
  • 从 PowerShell GitHub Releases 页面下载的 MSI 安装程序支持交互式安装,并提供将 PowerShell 添加到系统 PATH 的选项。
  • Winget 软件包管理器最适合基于脚本的部署和自动化。
  • 从 GitHub 下载的 ZIP 包提供了 PowerShell 的便携版本。它适用于没有管理员权限的环境,或用于测试。
  • 将 PowerShell 安装为 .NET Global 工具仍然是 .NET 开发人员的一个不错选择——尤其是那些安装并使用其他 Global 工具的开发人员。

适用于 macOS 的安装

  • 由于具备自动更新并与 macOS 工具集成,建议 macOS 用户使用 Homebrew。
  • 从 PowerShell GitHub Releases 页面下载的安装包(.pkg)提供交互式安装体验。

用于在 Linux 上安装

  • 使用发行版的包管理器安装 PowerShell,例如 Ubuntu/Debian 和 openSUSE。
  • Tarball 归档包提供可移植的安装方式。你可以从 GitHub 下载 .tar.gz 文件。

哪种安装方式更适合你?

选择合适的方法来安装 PowerShell 7 取决于所使用的平台以及你的具体需求。

Environment

Recommended Method

Windows Desktop

Microsoft Store or MSI Installer

Windows Server

MSI Installer or ZIP Package

macOS

Homebrew or .pkg Installer

Linux (Desktop)

Native Package Manager

Linux (Server)

Native Package Manager or Docker

Containerized Apps

Docker Images

Portable Needs

ZIP Package (Windows) or Tarball (Linux/macOS)

Developers

Build from Source

以下是一些额外建议:

  • 使用 Microsoft Store 或 Homebrew 可实现更简单的安装并获得自动更新。
  • 如果你没有管理员权限,请使用便携版(ZIP 或 Tarball)。
  • 为与配置管理工具实现更好的集成,请在 Windows 上选择 MSI 安装程序,在 Linux 上使用原生的软件包管理器。
  • Docker 非常适合隔离环境和可复现的部署设置,包括开发与测试环境。
  • 高级用户或为 PowerShell 做贡献的开发者可以克隆 PowerShell 的 GitHub 代码库,并从源代码进行编译。

在 Windows 上安装 PowerShell 7

使用 Windows 包管理器(Winget)

Windows 包管理器(Winget)是一种在 Windows 上安装 PowerShell 7 的简单且高效的方法。在 Windows 客户端上安装 PowerShell 时,这是推荐的方式。

先决条件

  • Winget 已预装在 Windows 11 以及更新的 Windows 10 版本中。如果它不可用,请从 Microsoft Store 上的 App Installer 包 安装。
  • 可以从命令提示符或 PowerShell 运行 Winget。
  • 以管理员身份运行 Winget 可确保安装过程不会因权限问题而中断。

使用 Winget 安装 PowerShell 7 的步骤

  1. 启动“命令提示符”或 PowerShell 其中之一。
  2. 使用包 ID 安装 PowerShell 的最新版本:

winget install –id Microsoft.PowerShell –source winget

要安装特定版本,请指定 –version 参数:

winget install –id Microsoft.PowerShell –version 7.4.6

从 Microsoft Store 安装

通过 Microsoft Store 安装 PowerShell 有多项优势。

  • 可确保自动更新。
  • 安装过程顺畅,并能与 Windows 无缝集成。
  • 提供可信且安全的来源,降低下载被篡改或非官方版本的风险。

通过 Microsoft Store 安装 PowerShell 7 的步骤

  1. 要打开 Microsoft Store,请按 Windows + S,输入 Microsoft Store 然后按 Enter。
  2. 在 Microsoft Store 的搜索栏中输入 PowerShell 7 ,然后按 Enter。
  3. 在搜索结果中查找 PowerShell(确保其提到 PowerShell 7 或最新版本)。为此,请向下滚动到“Additional Information(补充信息)”或“App Details(应用详情)”部分,并查看其中的版本号。
  4. 点击“Get(获取)”或“Install(安装)”按钮来安装 PowerShell。如果你尚未登录 Microsoft Store,可能需要使用你的 Microsoft 帐户进行登录。

通过 MSI 包安装 PowerShell

通过 MSI 包安装 PowerShell 在企业环境以及其他需要受控、可重复安装的场景中尤为有利。这使其成为 Windows 服务器和企业部署场景的推荐方法。

  • MSI 包提供集中式部署,IT 管理员可以使用 Group Policy、System Center Configuration Manager (SCCM) 或其他企业 软件部署工具 在多台计算机上进行部署。
  • 这也确保在所有系统中始终安装相同的 PowerShell 版本。
  • MSI 包支持离线安装。
  • 在安装过程中,MSI 包提供选项,用于配置诸如安装路径以及将 PowerShell 添加到系统路径等设置,从而便于根据特定需求定制安装方案。

通过 MSI 包安装 PowerShell 7 的步骤

  1. 进入 PowerShell GitHub releases page
  2. 找到最新的 PowerShell 7 版本,然后向下滚动到 Assets(资源)部分。
  3. 下载适用于您系统的相应 MSI 文件:
  4. 适用于 64 位系统的 PowerShell-x64.msi
  5. 适用于 32 位系统的 PowerShell-x86.msi
  6. 在系统中找到下载的 .msi 文件(通常在“下载”文件夹中),然后双击它以运行安装程序。按照屏幕上的说明安装 PowerShell。

ZIP 包安装

使用 ZIP 归档文件进行 PowerShell 的自定义安装。该方法灵活且易于操作,适用于无法使用安装程序或包管理器的环境。它也是安装多个版本的最简单方式,并且仍然是 Windows Nano Server、Windows IoT 和基于 Arm 的系统的推荐方法。

  1. 转到 PowerShell GitHub releases page
  2. 找到最新的 PowerShell 7 发行版本,然后向下滚动到“Assets”部分。
  3. 为你的系统下载相应的 ZIP 包:
  4. 适用于 64 位系统的 win-x64.zip
  5. 适用于 32 位系统的 win-x86.zip
  6. 进入下载 ZIP 文件的文件夹,然后将其内容解压到某个目录中。
  7. 将 PowerShell 添加到系统 PATH。
  8. 打开“控制面板”,依次导航到“系统 > 高级系统设置 > 环境变量”。
  9. 在“系统变量”下找到“Path”变量,然后单击“编辑”。
  10. 单击“新建”,并添加你解压 PowerShell 的目录路径(例如:C:\PowerShell\pwsh)。
  11. 单击“确定”以保存并关闭对话框。

要运行 PowerShell 7,请打开新的命令提示符或 PowerShell 窗口。然后输入 pwsh 并按 Enter 启动 PowerShell。

作为 .NET 全局工具安装

如果你已经安装了 .NET Core SDK ,你可以将 PowerShell 安装为 .NET 全局工具。

dotnet tool install –global PowerShell

dotnet 工具安装程序会将 $HOME\.dotnet\tools 添加到你的 $env:PATH 环境变量中。

命令行静默安装

PowerShell 的静默安装非常适合企业环境中的自动化部署。

通过命令行执行 PowerShell 7 静默安装的步骤

  1. 下载 MSI 安装程序,具体请参阅“通过 MSI 包安装 PowerShell”部分。
  2. 以管理员权限打开命令提示符。为此,请右键单击“命令提示符”,然后选择“以管理员身份运行”。
  3. 使用以下命令静默安装 PowerShell:
      msiexec /i PowerShell-x64.msi /quiet /norestart
      

例如:当 MSI 位于“下载”文件夹中时

      msiexec /i "%UserProfile%\Downloads\PowerShell-x64.msi" /quiet /norestart
      

在此命令中:

  • /i 表示安装
  • /quiet 会在不显示任何 UI 的情况下运行安装程序
  • /norestart 可防止系统在安装完成后自动重新启动

用于自动部署的高级命令行安装

如需进行 PowerShell 的高级命令行静默安装,管理员可以利用其他配置选项,以便在企业环境中更轻松地实现自动部署。下面是一些示例。

  • 使用 INSTALLFOLDER 属性指定自定义安装路径,如下所示:
    msiexec /i PowerShell-x64.msi /quiet /norestart INSTALLFOLDER=”C:\CustomPath\PowerShell7″
  • 按如下方式在文件资源管理器中添加“上下文菜单”:
    msiexec /i PowerShell-x64.msi /quiet /norestart ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=true
  • 按如下方式将 PowerShell 从系统 PATH 中排除:
    msiexec /i PowerShell-x64.msi /quiet /norestart DO_NOT_ADD_TO_PATH=true
  • 按如下方式通过 Windows Server Update Services (WSUS) 启用自动更新:
    msiexec /i PowerShell-x64.msi /quiet /norestart ENABLE_MU=true
  • 创建一个批处理文件或 PowerShell 脚本,用于大规模部署。

面向初学者的 Windows PowerShell 脚本教程(PDF)

了解更多

Install PowerShell 7 on macOS

You can install PowerShell on macOS using the following methods:

  • Using Homebrew
  • Downloading Installer Package (.pkg)
  • Installing as a .NET Global tool
  • 使用 ZIP 压缩包

在 macOS 上安装 PowerShell 之前,让我们先了解安装要求以及支持的 macOS 版本。

受支持的 macOS 版本

Microsoft 会在 PowerShell 到达支持终止(end-of-support)或 macOS 版本到达支持终止之前,一直支持 PowerShell。当前支持的版本包括:

  • macOS 15(Sequoia)x64 和 Arm64
  • macOS 14(Sonoma)x64 和 Arm64
  • macOS 13(Ventura)x64 和 Arm64

使用以下命令检查您的 macOS 版本:

sw_vers -productVersion

系统要求

系统要求如下:

Processor

Intel-based MacApple Silicon (M1/M2) is supported via Rosetta 2 or native ARM64 binaries (introduced in later releases of PowerShell 7)

Disk Space

At least 100 MB of free disk space for installation

Prerequisites

HomebrewAlternatively, use the .pkg installer from the GitHub release page

.NET Runtime

PowerShell 7 relies on the cross-platform .NET Core runtime. The required runtime is bundled with the PowerShell package, so no separate installation is required.

Apple Silicon (M1/M2) Considerations

PowerShell supports native execution on Apple Silicon starting with PowerShell 7.2. If using an earlier version, Rosetta 2 must be installed.

使用 Homebrew 安装 PowerShell

Homebrew 是 macOS 上很受欢迎的软件包管理器,并提供了一种简单的方法来安装和更新 PowerShell。

要使用 Homebrew 安装 PowerShell,请运行以下命令:

      brew install --cask powershell
      

也可以使用以下 cmdlet 安装 PowerShell 的 LTS 版本:

      brew install powershell/tap/powershell-lts
      

从 GitHub 下载软件包

Microsoft 提供用于 PowerShell 的已签名 .pkg 安装程序。使用它安装 PowerShell 适合偏好手动安装流程的用户。

  1. 转到 PowerShell GitHub releases page
  2. 找到最新的 PowerShell 7 版本,然后向下滚动到“Assets(资源)”部分。
  3. 下载适用于 macOS 的 .pkg 安装程序。
  4. 双击下载的 .pkg 文件,然后开始安装 PowerShell。

作为 .NET Global 工具安装

此方法使用 .NET Core SDK 来安装 PowerShell。

  1. 使用 Homebrew 下载并安装 .NET SDK:
      brew install --cask dotnet-sdk
      
  • 使用 .NET Core CLI 将 PowerShell 安装为全局工具:
      dotnet tool install --global PowerShell
      

dotnet 工具安装程序会将 ~/.dotnet/tools 添加到你的 PATH 环境变量中。--global 会将 PowerShell 以全局方式安装,这样你就可以在整个系统范围内访问它。

使用 ZIP 存档进行安装

这种方法不太常见,但可以用于自定义设置。

  1. 进入 PowerShell GitHub releases page
  2. 找到最新的 PowerShell 7 版本,并向下滚动到 Assets(资源)部分。
  3. 下载适用于 macOS 的 ZIP 文件。通常命名为:powershell-7.x.x-osx-x64.tar.gz。
  4. 将 ZIP 文件解压到某个目录中。
  5. 使用以下命令使 pwsh 二进制文件可执行:
      chmod +x ~/powershell/pwsh
      
  • 将该目录添加到你的 $PATH,或在已包含于你的 $PATH 的目录中为 pwsh 创建一个符号链接:
      ln -s ~/powershell/pwsh /usr/local/bin/pwsh
      
  • 最后,你可以使用 pwsh cmdlet 运行 PowerShell。

在 Linux 上安装 PowerShell 7

PowerShell 支持多种 Linux 发行版,能够在不同环境中使用。下表列出了用于安装 PowerShell 的官方支持发行版。

Distribution

Versions

Package Type

Notes

Ubuntu

20.04, 22.04

.deb

Widely supported and tested. Uses the official Microsoft APT repository.

Debian

10, 11

.deb

Installable via the Microsoft APT repository.

CentOS

7, 8 Stream

.rpm

Red Hat-derived. Available via the Microsoft YUM repository.

RHEL

7, 8, 9

.rpm

Supported using YUM or DNF from the Microsoft repository. Requires enabling “Optional” and “Extras” repos on RHEL 7.

Fedora

37, 38

.rpm

Supported via YUM/DNF repositories.

Alpine

3.15+

Binary tar.gz

PowerShell for Alpine is available but may require additional steps due to differences in libraries (for example, glibc compatibility).

Arch Linux

Rolling Release

AUR

Supported unofficially via the Arch User Repository (AUR).

Kali Linux

Rolling Release

.deb

Installed via the Microsoft APT repository, similar to Debian/Ubuntu.

Linux 的标准安装方式

在 Linux 上安装 PowerShell 7 的标准方法如下:

  • 软件包管理器(APT、YUM、DNF)– 对于 Debian/Ubuntu,请使用来自 Microsoft APT 仓库的 .deb 软件包。对于 RHEL/CentOS/Fedora,请使用来自 Microsoft YUM 仓库的 .rpm 软件包。
  • 二进制归档安装——对于像 Alpine Linux 这样的发行版或自定义环境,请使用来自 PowerShell GitHub 发布的 .tar.gz 归档。

通过软件包管理器安装

APT(Ubuntu/Debian)

  1. 开始之前,请使用以下命令更新系统的包列表:
      sudo apt update

sudo apt upgrade -y
      
  • 使用以下命令安装前置条件:
      sudo apt install -y wget apt-transport-https software-properties-common
      
  • 使用以下命令导入 Microsoft GPG 密钥并注册仓库:
      wget -q https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb

sudo dpkg -i packages-microsoft-prod.deb
      
  • 使用以下命令安装 PowerShell:
      sudo apt-get update

sudo apt-get install -y powershell
      

YUM/DNF (CentOS/RHEL/Fedora)

  1. 使用以下命令导入 Microsoft GPG 密钥并添加仓库:
      sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc

curl https://packages.microsoft.com/config/rhel/8/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
      
  • 使用此命令安装 PowerShel:
      sudo yum install -y powershell
      

Alpine Linux

  1. 使用以下命令安装所需依赖项:
      apk add --no-cache bash icu-libs less curl
      
  • 使用以下命令下载并解压 PowerShell:
      wget https://github.com/PowerShell/PowerShell/releases/download/v<version>/powershell-<version>-linux-alpine-x64.tar.gz

mkdir ~/powershell

tar -xzf powershell-<version>-linux-alpine-x64.tar.gz -C ~/powershell
      
  • 然后,按下例所示,进入该目录并执行 PowerShell 二进制文件即可运行 PowerShell:
      ~/powershell/pwsh
      

使用二进制归档(tar.gz)进行安装

.tar.gz 归档包具有良好的可移植性,可在几乎任何 Linux 系统上运行。

  1. 访问 PowerShell GitHub releases page 并下载适合你架构的 tar.gz 文件(x64 或 ARM64)。下面是示例命令:
      wget https://github.com/PowerShell/PowerShell/releases/download/v7.4.0/powershell-7.4.0-linux-x64.tar.gz
      
  • 使用以下命令解压归档:
      mkdir ~/powershell

tar -xzf powershell-7.4.0-linux-x64.tar.gz -C ~/powershell
      
  • 使用以下命令运行 PowerShell:
      ~/powershell/pwsh
      

社区与其他安装选项

在 Linux 上安装 PowerShell 的其他选项包括:

  • Docker 容器 — 使用适用于 Linux 发行版的官方 PowerShell Docker 镜像。
  • 手动构建 — 对于不受支持或自定义的发行版,可以从源代码构建 PowerShell,但这需要专业知识和时间。

使用 Docker 安装

PowerShell 在 Microsoft Container Registry (MCR) 中发布了官方 Docker 镜像,支持 Linux 发行版。

  1. 使用以下命令拉取 PowerShell 镜像:
      docker pull mcr.microsoft.com/powershell
      
  • 要在容器中启动交互式 PowerShell 会话,请使用以下命令:
      docker run -it mcr.microsoft.com/powershell
      

PowerShell 在 Linux 上的 ARM 支持

PowerShell 支持基于 ARM 的 Linux 系统,尤其是 ARM64 架构。支持 ARM 的发行版包括:

  • Ubuntu 20.04、22.04(ARM64)
  • Debian 11 (ARM64)
  • RHEL 8/9 (ARM64)
  • Alpine 3.15+ (ARM64)

在 ARM 上安装 PowerShell(以 Ubuntu 为例),请执行以下操作:

  1. 使用以下命令从 PowerShell GitHub releases page 下载 ARM64 .deb 软件包:
      wget https://github.com/PowerShell/PowerShell/releases/download/v7.4.0/powershell_7.4.0-1.deb_arm64.deb
      
  • 使用以下命令安装该软件包:
      sudo dpkg -i powershell_7.4.0-1.deb_arm64.deb
      

从源代码安装

对于不受支持或高度定制的 Linux 环境,您可以从源代码构建 PowerShell。

  1. 使用以下命令克隆 PowerShell 仓库:
      git clone https://github.com/PowerShell/PowerShell.git

cd PowerShell
      
  • 安装依赖项(因发行版而异)。
  • 使用以下方式构建 PowerShell:
      ./build.sh -c Release
      

这里,-c Release 指定构建配置为“Release”(针对生产环境进行了优化)。

  • 生成的 pwsh 二进制文件将位于 out/ 目录中(./out/linux-x64/)。
  • 要安装 PowerShell:
  • 为 PowerShell 创建一个目录(例如:/usr/local/bin/powershell)。
      sudo mkdir -p /usr/local/bin/powershell

sudo cp -r ./out/linux-x64/* /usr/local/bin/powershell/
      
  • 创建一个符号链接,以便让 pwsh 可全局访问:
      sudo ln -s /usr/local/bin/powershell/pwsh /usr/bin/pwsh
      

运行和使用 PowerShell 7

启动 PowerShell 7

在 Windows、macOS 和 Linux 上启动 PowerShell 7 的方式有多种。

在 Windows 上

Method

Steps


Start Menu

Press the Windows key or click the Start Menu.Type pwsh and press Enter.

Run Dialog

Press Win + R to open the Run dialog.Type pwsh and click OK.

Command Prompt or Windows PowerShell

Open Command Prompt (cmd) or Windows PowerShell.Type pwsh and press Enter.

Desktop Shortcut

Create a shortcut pointing to the pwsh.exe executable (typically found in C:\Program Files\PowerShell\7\pwsh.exe).Double-click the shortcut to open PowerShell 7.

Context Menu

Right-click on an empty space in a folder while holding Shift.Select Open PowerShell window here (if configured).

Taskbar or Start Pin

Locate PowerShell 7 in the Start Menu.Right-click and choose Pin to Start or Pin to Taskbar for easy access.

在 macOS 上

Method

Steps

Terminal

Open the built-in Terminal app.Type pwsh and press Return.

Spotlight Search

Press Cmd + Space to open Spotlight.Type pwsh and press Return.

Finder

Navigate to the folder where PowerShell 7 is installed (for example, /usr/local/bin if installed via Homebrew).Double-click or run it from the Terminal.

Homebrew (if installed via Homebrew)

Use the command: brew list | grep powershell

在 Linux 上

Method

Steps

Terminal

Open a terminal.Type pwsh and press Enter.

Application Menu

Depending on your desktop environment (for example, GNOME, KDE), search for “PowerShell” in the application menu.

Desktop Shortcut

Create a desktop shortcut to the pwsh executable (typically located in /usr/bin/pwsh or /opt/microsoft/powershell/7/pwsh).

Run Command

Use the Alt + F2 (GNOME) or Super + R (KDE) run command dialog.Type pwsh and press Enter.

Script or Cron Job

Add pwsh to your scripts or scheduled tasks for automation.

基本命令用法和提示

以下是帮助你快速上手的基本命令用法和提示。

基本命令语法

Item

Syntax

Cmdlets

PowerShell uses cmdlets (pronounced “command-lets”) with the structure: Verb-Noun -ParameterName Value An example is: Set-Item -Path “C:\test.txt” -Value “Hello”

Pipelines

Use the pipe | to send the output of one cmdlet as input to another. Here is an example: Get-Process | Where-Object { $_.CPU -gt 100 }

基础命令

Task

Command

Find Help

To get help for a cmdlet: Get-Help Get-Process
To view examples: Get-Help Get-Process-Examples

Navigate the File System

List directory contents: Get-ChildItem
Change directory: Set-Location C:\Path
Create a new directory: New-Item -ItemType Directory -Path C:\NewFolder

Manage Files

Create a new file: New-Item -ItemType File -Path C:\test.txt
Read file content: Get-Content C:\test.txt
Write to a file: Set-Content -Path C:\test.txt -Value “Hello, World!”

View System Information

Check system processes: Get-Process View running services: Get-Service
Get system information: Get-ComputerInfo

Scripting Basics

Run a script: .\script.ps1
Variables: $name = “PowerShell”
Write-Output “Hello, $name!”

Package Management

Install a module: Install-Module -Name Az -Scope CurrentUser
Import a module: Import-Module Az
List installed modules: Get-InstalledModule

高效使用技巧

自动补全

使用 Tab 键可自动补全 cmdlet、参数或文件路径。

别名

如下面所示,PowerShell 支持 cmdlet 的快捷方式:

      dir      # Alias for Get-ChildItem

cd       # Alias for Set-Location

cls      # Alias for Clear-Host
      

要查看所有别名,请使用以下 cmdlet:

      Get-Alias
      

脚本中的注释

  • 单行注释如下:
      # This is a comment
      
  • 多行注释如下:
      <#

This is a multi-line comment

#>
      

错误处理

try-catch 代码块非常适合进行错误处理,具体如下:

      Try {

    Get-Item "C:\nonexistent.txt"

} Catch {

    Write-Host "An error occurred: $_"

}
      

输出格式

  • 使用以下 cmdlet 将输出格式化为表格:
      Get-Process | Format-Table
      
  • 使用以下 cmdlet 将输出格式化为列表:
      Get-Process | Format-List
      

跨平台提示

  • 在非 Windows 系统上,你仍然可以使用诸如 ls 或 cat 这样的原生 shell 命令。
  • 使用 pwsh -WindowsCompatibility 模式来调用某些仅限 Windows 的模块。

保持 PowerShell 7 始终为最新版本

根据安装方式选择更新选项

在 Windows 上更新 PowerShell

根据 PowerShell 最初的安装方式,你可以使用 Winget、Microsoft Store 或 MSI 安装程序来进行更新。

Method


Description


Winget

Winget doesn’t automatically update installed applications, but you can manually update PowerShell. Open PowerShell as an administrator and run the following command: winget upgrade –id Microsoft.PowerShell –source winget Here, –source winget forces the command to only use the winget source repository when finding the PowerShell package. This ensures that the package is not sourced from other repositories you might have added to winget, such as third-party repositories or custom sources.

Microsoft Store

Open the Microsoft Store and type “PowerShell” in the search bar to search for PowerShell. Select the “PowerShell” application. If an update is available, the Update button will appear. Click it to update PowerShell.

MSI installer

Download the appropriate MSI file for your system architecture from the PowerShell GitHub releases page. Double-click the downloaded MSI file and follow the on-screen instructions to update PowerShell. Restart PowerShell when the update completes.

在 macOS 上更新 PowerShell

根据 PowerShell 最初的安装方式,你可以使用多种方法在 macOS 上更新 PowerShell。

Method

Description

Homebrew

Update Homebrew to ensure it has the latest package information using the following cmdlet: brew update Run the following command to upgrade PowerShell: brew upgrade powershell

Manual Download (GitHub)

Download the .pkg file for the latest version of PowerShell from the PowerShell GitHub releases page. Double-click the downloaded .pkg file and follow the on-screen instructions to update PowerShell.

.NET Tool

Make sure the .NET CLI is installed. Then use the following command to update PowerShell: dotnet tool update –global PowerShell

在 Linux 上更新 PowerShell

在 Linux 上更新 PowerShell 取决于它是如何安装的。

Method

Description

Package Manager

For APT-based Systems (Ubuntu, Debian) Update the Package List: sudo apt update Update PowerShell: sudo apt upgrade powershell For DNF-based Systems (Fedora, CentOS, RHEL) Update the Package Repository: sudo dnf check-update Update PowerShell: sudo dnf upgrade powershell

Manual Download (GitHub)

Download the latest package for PowerShell as appropriate to your Linux distribution (for example, .deb, .rpm, or binary .tar.gz) from the PowerShell GitHub releases page. Uninstall the previous PowerShell version and then use the following cmdlets to install the new version. For APT-based systems sudo dpkg -i powershell-<version>.deb For DNF-based systems sudo rpm -Uvh powershell-<version>.rpm For binary .tar.gz tar -xvf powershell-<version>.tar.gz sudo mv pwsh /usr/local/bin

从旧版本升级

从 PowerShell 5.1(Windows PowerShell)或 PowerShell Core 升级到最新的 PowerShell 7+ 版本需要特定步骤和注意事项。例如,应测试现有脚本以确保它们能在 PowerShell 7+ 中正常工作,并备份存储在 $PROFILE 中的任何自定义配置文件或脚本。

从 Windows PowerShell 5.1 升级

Windows PowerShell 5.1 不能直接升级,但可以与 PowerShell 7+ 共存。

从 PowerShell Core(6.x)升级

PowerShell Core 可以直接升级到 PowerShell 7+。

  1. 请访问 PowerShell GitHub 发布页面 并下载与您的操作系统兼容的安装包(例如:Windows 用 .msi,Linux 用 .deb)。
  2. 安装新版本。有关详细信息,请参阅相关部分:
  3. 在 Windows 上安装 PowerShell 7
  4. 在 macOS 上安装 PowerShell 7
  5. 在 Linux 上安装 PowerShell 7

默认情况下,如果在同一位置安装,PowerShell 7+ 将替换 PowerShell Core 的安装。

PowerShell 7 的支持与兼容性

受支持的操作系统

PowerShell 7 是跨平台的,可在 Windows、macOS 以及各种 Linux 发行版上运行。

Windows

Supported Versions

Windows 10 (x64 and ARM64) – Version 1607 or laterWindows 11 – All versionsWindows Server – 2012 R2 or later

Requirements

.NET Runtime – PowerShell 7 is built on .NET 6 or later. These runtimes are bundled with the PowerShell 7 installer for Windows.Disk Space – Approximately 150 MB for the base installationRAM – Minimum 1 GB; 2 GB or more is recommended

macOS

Supported Versions

macOS 10.13 High Sierra or later (64-bit)

Requirements

.NET Runtime – .NET 6 or later runtime is bundled with the PowerShell 7 package for macOSDisk Space – Approximately 150 MB for the installationRAM – Minimum 1 GB; 2 GB or more is recommended

Linux

Requirements

.NET Runtime – .NET 6 or later runtime is required. It is bundled in most PowerShell 7 packages or can be installed manually.Disk Space – Approximately 150 MB for the installationRAM – Minimum 1 GB; 2 GB or more is recommended

Supported Distributions

PowerShell 7 supports a wide range of Linux distributions. The most common ones are: Debian-based (APT package manager) Ubuntu 16.04 or later (64-bit) Debian 9 or later (64-bit) Red Hat-based (DNF/YUM package manager) CentOS 7 or later (64-bit) Red Hat Enterprise Linux (RHEL) 7 or later (64-bit) Fedora 30 or later (64-bit) SUSE-based (Zypper package manager) openSUSE 42.3 or later (64-bit) SUSE Linux Enterprise Server (SLES) 12 SP2 or later (64-bit) Other Alpine Linux 3.10 or later (64-bit) Arch Linux (community-maintained)

跨平台配置

Supported Hardware Architecture

x64ARM64 (for supported Windows and Linux distributions)

Compatibility Features

Windows-specific modules (for example, Active Directory, Exchange) are supported via the compatibility module (Enable-WindowsCompatibility) on Windows.Cross-platform remoting is supported using SSH or WinRM, depending on the platform.

容器支持

Docker Images

Official PowerShell Docker images are available for Linux, Windows, and ARM-based systems. Images are available for: Debian Ubuntu Alpine Windows Server Core

Requirements

Docker must be installed and configured.

不受支持的配置

  • PowerShell 7 仅支持 64 位操作系统,不支持 32 位操作系统。
  • PowerShell 7 不支持早于 Windows 10 的 Windows 版本。
  • 不支持早于 10.13 High Sierra 的 macOS 版本。
  • 已进入生命周期终止(end-of-life)状态的 Linux 发行版不被官方支持。

Microsoft Update 集成

在 Windows 上,可以通过 Microsoft Update 管理 PowerShell 7 更新,从而让用户能够与其他 Windows 更新一起自动接收更新。

通过 Microsoft Update 获取 PowerShell 7 更新的工作原理

  • Microsoft Update——PowerShell 7 与 Microsoft Update 集成。Microsoft Update 是一项可选服务,可为包括 PowerShell 在内的 Microsoft 软件提供更新。
  • 更新类型 – 更新可能包括新版本、错误修复、安全补丁和改进功能。
  • 频率 – 更新会根据 PowerShell 的开发周期以及关键安全更新,定期发布。

启用 Microsoft Update

确保您的系统已配置为使用 Microsoft Update。

  1. 进入 Windows 更新,然后点击“高级选项”。
  2. 在“接收其他 Microsoft 产品的更新”中,将开关切换为“开”。此设置允许 Microsoft Update 检查 PowerShell 及其他 Microsoft 软件的更新。

设置自动更新

为确保 PowerShell 更新能够自动安装,请配置 Windows 更新设置。

  1. 进入“Windows 更新”,然后点击“高级选项”。
  2. 配置以下内容:
  3. 自动更新 – 启用更新以自动安装
  4. 活动时段 – 设置活动时段,以防在工作时间进行更新

Active Directory 组管理最佳实践

了解更多

PowerShell 7 安装问题排查

在不同平台上安装 PowerShell 7 有时可能会因各种环境或依赖性问题而出现错误。以下汇总了 Windows、macOS 和 Linux 平台上的常见错误及其解决方法。

Windows

  1. 错误:执行策略阻止安装

此错误发生是因为执行策略过于严格。在尝试安装之前,请在提升权限的 PowerShell 会话中运行以下命令:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

  • 错误:缺少先决条件,无法安装

此错误发生的原因是缺少 .NET Core Runtime。您必须从 Microsoft’s .NET site 下载并安装最新的 .NET Core Runtime,然后继续进行 PowerShell 安装。

  • 错误:安装程序需要管理员权限

此错误是由于缺少管理员权限造成的。要解决它,请右键单击安装程序并选择“以管理员身份运行”。

MacOS

  1. 错误:zsh:未找到命令:pwsh

发生此错误的原因是 PowerShell 二进制文件不在系统的 PATH 中。运行以下命令以解决问题:

echo ‘export PATH=”/usr/local/microsoft/powershell/7:$PATH”‘ >> ~/.zshrc

source ~/.zshrc

  • 错误:无法挂载已下载的 .pkg 文件

发生此错误的原因是 .pkg 文件已损坏。请删除该文件,然后从 官方网站 重新下载安装。

  • 错误:安装需要更新版本的 macOS

此错误发生是因为 macOS 版本过旧。你需要将 macOS 升级到 PowerShell 7 支持的版本。

  • 错误:安装过程中权限被拒绝

此错误是由于缺少管理员权限导致的。请使用以下命令解决问题:

sudo installer -pkg powershell-7.x.x.pkg -target /

Linux

  1. 错误:无法找到 powershell 包

出现此错误是因为尚未将 Microsoft 仓库添加到你的软件包管理器中。使用以下命令将其添加:

# Ubuntu/Debian 示例:

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb

sudo dpkg -i packages-microsoft-prod.deb

sudo apt-get update

  • 错误:缺少依赖项

此错误发生是因为未安装诸如 libssl 或 curl 之类的依赖项。要安装缺失的依赖项,请使用以下命令:

# Ubuntu 示例:

sudo apt-get install -y libssl1.1 libcurl4

  • 错误:运行 pwsh 时权限被拒绝

该错误是由于二进制文件权限不足导致的。运行以下命令以解决问题:

sudo chmod +x /usr/bin/pwsh

  • 错误:软件包的版本与系统架构不兼容

出现此错误是因为下载的安装包架构不正确,例如 x86 与 ARM 的区别。你必须从 official release page 下载与你系统架构相匹配的正确安装包。

解决安装和运行时问题的提示

以下是一些通用提示,可用于排查并解决跨平台的 PowerShell 7 安装和运行时问题。

通用安装提示

核实系统要求

  • 确认你的操作系统版本和架构(x86、x64、ARM)支持 PowerShell 7。
  • 确保已安装所需的依赖项,例如 .NET Core Runtime。

从官方来源下载

下载 PowerShell 软件包时,请始终使用官方 PowerShell GitHub releases page 或你所在平台可信的仓库。

检查安装日志

如需了解故障详情,请查看日志:

  • Windows – C:\Windows\Temp 或 事件查看器
  • Linux/macOS – 终端错误消息或软件包管理器日志

更新软件包管理器

在运行安装命令之前,请确保软件包管理器(例如 apt、yum、brew)是最新版本。

确保正确配置 PATH

安装完成后,确认可以通过 PATH 访问 PowerShell 二进制文件。如不能,请手动添加:

  • Windows – 编辑环境变量以包含 C:\Program Files\PowerShell\7
  • Linux/macOS – 将 /usr/bin/pwsh 或 /usr/local/bin/pwsh 添加到你的 shell 配置中

使用提升的权限

在需要管理员/超级用户权限的平台上,请执行以下操作:

  • Windows – 以管理员身份运行
  • Linux/macOS – 使用 sudo 安装软件包

解决运行时问题的提示

  • 如果 PowerShell 卡住或表现异常,请跳过配置脚本(profile scripts)。
  • 确保 PowerShell 以及已安装的所有模块都是最新版本。
  • 暂时移除第三方模块或脚本,以识别兼容性问题。
  • 查看系统日志以获取更详细的错误信息。
  • 如果错误与远程会话或模块安装有关,请确认互联网连接以及 DNS 解析是否正常。同时检查防火墙规则和代理设置。
  • 确保正确设置 PowerShell 环境变量,例如 $PSModulePath 和 $env:PATH。

结论

本文讨论了在包括 Windows、macOS 和 Linux 在内的各种平台上安装和更新 PowerShell 7 的不同方法。根据用户需求,以下是一些安装 PowerShell 7 的更多建议:

适合初学者/日常用户

  • Windows – 通过 MSI 或 Microsoft Store 安装
  • macOS – 使用 .pkg 安装程序
  • Linux – 通过本机包管理器安装

适用于开发人员/高级用户

  • 使用包管理器(Homebrew、apt、dnf)以实现高效更新并与系统集成
  • 对于跨平台脚本,请安装到一致的目录(例如 /usr/local/bin)

用于便携式安装,或在缺少管理员权限时

  • Windows – 使用 ZIP 压缩包
  • Linux/macOS – 手动安装请使用 tarball

适用于企业环境

  • 使用集中式部署工具(例如 Group Policy、SCCM)来部署 PowerShell MSI 文件
  • 在各系统上自动化安装脚本

适用于互联网访问受限的环境:

  • 下载离线安装程序(MSI/PKG/Tarball)并在本地分发

按照以下指南,您可以在不同环境中最大化 PowerShell 7 的能力:

  • 使用 $IsWindows、$IsLinux 和 $IsMacOS 自动变量编写与平台无关的脚本。另外,您还应将特定环境的路径存储到变量中,以避免硬编码。
  • 为提高效率自定义您的 $PROFILE。
  • 使用 PowerShell Remoting 来管理远程计算机。请确保启用了跨平台 SSH 远程连接。
  • 排查问题时,请使用内置的错误处理,并启用详细日志记录。
  • 在生产环境中使用脚本时,请始终对脚本进行签名。
  • PowerShell 7 擅长与 API 交互,因此请使用 REST API 和 Web 请求。
  • 将 PowerShell 集成到 Jenkins、Azure DevOps 和 GitHub Actions 等自动化工具中。
  • 编写模块化脚本和可复用函数,并将它们打包成模块。

Netwrix Directory Manager

常见问题

分享到

了解更多

关于作者

Asset Not Found

Jonathan Blackwell

软件开发负责人

自 2012 年以来,工程师兼创新者 Jonathan Blackwell 一直提供工程领导力,使 Netwrix GroupID 成为 Active Directory 和 Azure AD 环境中群组与用户管理领域的领先者。他在研发、市场与销售方面的经验,使 Jonathan 能够全面理解 Identity 市场以及买家如何思考。