PowerShell 获取与安装:逐步指南 – wiki基地

PowerShell 获取与安装:逐步指南

1. 引言

PowerShell 是一种跨平台的任务自动化和配置管理框架,由 Microsoft 开发。它包含一个命令行 Shell、一种脚本语言以及一个配置管理框架。PowerShell 最初专为 Windows 系统设计,但现在已支持 Linux、macOS 以及其他平台,成为系统管理员、开发人员和DevOps工程师不可或缺的工具。

本指南将详细介绍如何在不同的操作系统上获取和安装 PowerShell。

2. 安装前准备

在开始安装之前,请确保您的系统满足以下基本要求:

  • 操作系统兼容性:确保您的操作系统版本受 PowerShell 支持。
  • 管理员权限:安装 PowerShell 通常需要管理员权限。
  • 网络连接:部分安装方法需要下载文件,因此需要稳定的网络连接。

3. 在 Windows 上安装 PowerShell

在 Windows 上安装 PowerShell 有多种方法,具体取决于您的 Windows 版本。

3.1 使用 Winget(推荐,适用于 Windows 10/11)

Winget 是 Windows 上的包管理器,可以简化软件的安装过程。

  1. 打开终端
    按下 Win + R,输入 cmdpowershell,然后按下 Ctrl + Shift + Enter 以管理员身份运行。
  2. 查找 PowerShell
    运行以下命令以查找可用的 PowerShell 版本:
    powershell
    winget search Microsoft.PowerShell
  3. 安装 PowerShell
    选择最新稳定版进行安装(例如,Microsoft.PowerShell):
    powershell
    winget install --id Microsoft.PowerShell --source winget

    如果您想安装预览版,可以使用 Microsoft.PowerShell.Preview

3.2 使用 MSI 安装包

这是 Windows 上最常见的安装方法,适用于所有受支持的 Windows 版本。

  1. 下载安装包
    访问 GitHub 上的 PowerShell 发布页面:https://github.com/PowerShell/PowerShell/releases
    在最新稳定版下找到 Assets 部分,下载适合您系统架构的 .msi 文件(例如,PowerShell-7.x.x-win-x64.msi 用于 64 位系统)。
  2. 运行安装程序
    双击下载的 .msi 文件,按照屏幕上的指示完成安装。通常,您可以接受默认设置。
  3. 验证安装
    安装完成后,打开一个新的 PowerShell 窗口,输入 pwsh 并按 Enter。您应该能看到 PowerShell 的版本信息。

3.3 通过 Microsoft Store 安装(适用于 Windows 10/11)

您也可以从 Microsoft Store 安装 PowerShell。

  1. 打开 Microsoft Store
    在 Windows 搜索栏中输入 “Microsoft Store” 并打开。
  2. 搜索 PowerShell
    在 Store 中搜索 “PowerShell”。
  3. 安装
    选择 “PowerShell” 应用(通常由 “Microsoft Corporation” 发布),然后点击 “获取” 或 “安装”。

4. 在 Linux 上安装 PowerShell

PowerShell 支持多种 Linux 发行版。这里以 Ubuntu/Debian 和 CentOS/RHEL 为例。

4.1 Ubuntu/Debian

  1. 更新包列表
    bash
    sudo apt update
  2. 安装依赖项
    bash
    sudo apt install -y wget apt-transport-https software-properties-common
  3. 导入 Microsoft GPG key
    bash
    wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
  4. 注册 Microsoft 存储库
    根据您的 Ubuntu/Debian 版本,选择相应的命令。例如,对于 Ubuntu 20.04 (Focal):
    bash
    sudo add-apt-repository "$(wget -q https://packages.microsoft.com/config/ubuntu/20.04/prod.list -O-)"

    对于 Debian 11 (Bullseye):
    bash
    sudo add-apt-repository "$(wget -q https://packages.microsoft.com/config/debian/11/prod.list -O-)"

    请根据实际情况替换 ubuntu/20.04debian/11
  5. 安装 PowerShell
    bash
    sudo apt update
    sudo apt install -y powershell

4.2 CentOS/RHEL

  1. 注册 Microsoft 存储库
    bash
    sudo curl -o /etc/yum.repos.d/microsoft.repo https://packages.microsoft.com/config/rhel/8/prod.repo

    请根据您的 RHEL/CentOS 版本调整 /rhel/8
  2. 安装 PowerShell
    bash
    sudo yum install -y powershell

    或者对于较新的系统使用 dnf:
    bash
    sudo dnf install -y powershell

5. 在 macOS 上安装 PowerShell

macOS 用户可以通过 Homebrew 或直接下载 PKG 文件进行安装。

5.1 使用 Homebrew(推荐)

  1. 安装 Homebrew(如果尚未安装):
    bash
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. 安装 PowerShell
    bash
    brew install --cask powershell

    如果您想安装预览版:
    bash
    brew install --cask powershell-preview

5.2 使用 PKG 安装包

  1. 下载安装包
    访问 GitHub 上的 PowerShell 发布页面:https://github.com/PowerShell/PowerShell/releases
    在最新稳定版下找到 Assets 部分,下载 PowerShell-7.x.x-osx-x64.pkg 文件(用于 Intel Mac)或 PowerShell-7.x.x-osx-arm64.pkg(用于 Apple Silicon Mac)。
  2. 运行安装程序
    双击下载的 .pkg 文件,按照屏幕上的指示完成安装。

6. 验证安装

无论您在哪种操作系统上安装 PowerShell,都可以通过运行以下命令来验证安装是否成功:

  1. 打开终端或 PowerShell 窗口
  2. 输入 pwsh 并按 Enter。
    如果安装成功,您将看到 PowerShell 的命令行提示符和版本信息,例如:
    PowerShell 7.4.1
    PS /home/user>
  3. 输入 $PSVersionTable 并按 Enter。
    这将显示更详细的 PowerShell 版本信息。

7. 更新 PowerShell

保持 PowerShell 最新以获取新功能和安全补丁非常重要。

  • Windows (Winget)
    powershell
    winget upgrade Microsoft.PowerShell
  • Windows (MSI)
    下载最新版的 MSI 安装包并重新运行安装程序。
  • Linux (apt/yum/dnf)
    使用系统的包管理器进行更新:sudo apt upgrade powershellsudo yum update powershell
  • macOS (Homebrew)
    bash
    brew upgrade --cask powershell

8. 结论

通过本指南,您现在应该能够在您选择的操作系统上成功安装和运行 PowerShell 了。PowerShell 的跨平台特性使其成为管理和自动化各种环境的强大工具。现在,您可以开始探索 PowerShell 的强大功能,提高您的工作效率。

滚动至顶部