Ngày nay, PowerShell là một shell và ngôn ngữ scripting mã nguồn mở đa nền tảng có thể cài đặt và sử dụng trên Windows, Linux, macOS và các nền tảng khác. Trong bài viết này, chúng tôi sẽ chỉ cách cài đặt PowerShell Core trên các bản phân phối Linux phổ biến (CentOS, RHEL, Debian, Kali, Mint, Ubuntu). Bài viết có thể hữu ích cho quản trị viên Windows quen thuộc với PowerShell để mang kinh nghiệm sang Linux.
- Danh sách đầy đủ gói PowerShell đã biên dịch cho các nền tảng khác nhau có sẵn trong kho Microsoft chính thức: https://github.com/PowerShell/PowerShell/releases
- Bạn có thể tìm thêm thông tin về hỗ trợ PowerShell trong các bản phân phối và phiên bản Linux tại: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-linux?view=powershell-7.2
Hiện tại, gói PowerShell bị thiếu trong hầu hết kho Linux mặc định. Vì vậy, để cài đặt PowerShell trên Linux, bạn phải tải gói kho Microsoft trước, nhập khóa, và dùng trình quản lý gói Linux để cài PowerShell.
Lưu ý rằng chỉ PowerShell Core (dựa trên .NET Core) có thể cài trên Linux. Bạn không thể cài Windows PowerShell 5.1 cổ điển trên Linux. Phiên bản PowerShell Core mới nhất tại thời điểm viết là PowerShell Core 7.2 LTS.
Cách cài đặt PowerShell Core trên Ubuntu 20.04 & Linux Mint 20?
Trước khi cài PowerShell trên Ubuntu hoặc Linux Mint, cập nhật danh sách gói:
sudo apt-get update -y
Tải khóa GPG của kho Microsoft:
wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
Cài khóa GPG Microsoft:
sudo dpkg -i packages-microsoft-prod.deb
Cập nhật danh sách gói với gói từ packages.microsoft.com:
sudo apt-get update
Cài PowerShell:
sudo apt-get install powershell -y

Gói PowerShell sẽ tự động cập nhật bởi trình quản lý gói apt:
sudo apt update
Nếu phiên bản PowerShell Core cũ đã cài trên máy, nó sẽ bị gỡ.
Bạn cũng có thể tải và cài gói DEB PowerShell thủ công:
wget -q https://github.com/PowerShell/PowerShell/releases/download/v7.2.0/powershell-lts_7.2.0-1.deb_amd64.deb
sudo dpkg -i powershell_7.2.0-1.deb_amd64.deb
Giải quyết phụ thuộc và hoàn tất cài đặt:
sudo apt-get install -f
Cài đặt PowerShell Core trên Debian 11
Để cài PowerShell Core 7.0+ trên Debian 11 Bullseye, tải khóa GPG cho kho Microsoft trước:
wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb
Sau đó thêm khóa GPG đã tải:
sudo dpkg -i packages-microsoft-prod.deb
Cập nhật danh sách ứng dụng trong trình quản lý gói:
sudo apt update
Cài PowerShell:
sudo apt install -y powershell

PowerShell sẽ tự động cập nhật khi bạn chạy cập nhật gói bằng lệnh:
sudo apt update
Nếu bạn chỉ muốn cập nhật gói PowerShell:
sudo apt install powershell
Cách cài đặt PowerShell trên Red Hat Enterprise Linux (RHEL) và CentOS?
PowerShell trên Linux CentOS và Red Hat Enterprise Linux (RHEL), Oracle Linux cài đặt theo cách tương tự:
Thêm kho Microsoft RedHat vào YUM:
curl https://packages.microsoft.com/config/rhel/8/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
Cài phiên bản PowerShell Core mới nhất bằng trình quản lý gói yum (hoặc dnf):
sudo yum install -y powershell

Bạn có thể cập nhật gói PoSh bằng lệnh:
sudo yum update powershell
Cũng có thể cài PowerShell từ file RPM:
sudo yum install https://github.com/PowerShell/PowerShell/releases/download/v7.2.0/powershell-lts-7.2.0-1.rh.x86_64.rpm
Cài đặt PowerShell Core 7.x trên Kali Linux
Để cài PowerShell trên Kali, chỉ cần chạy lệnh:
apt update && apt -y install powershell
Cài đặt PowerShell trên Linux với Snap
Bạn có thể dùng snap để cài PowerShell trên các bản phân phối Linux khác. Phương pháp này khuyến nghị cho các bản phân phối không có hỗ trợ PoSh chính thức.
Lệnh cài PowerShell bằng snap:
sudo snap install powershell --classic
Trong snap, PowerShell tự động cập nhật, nhưng bạn cũng có thể làm thủ công:
sudo snap refresh powershell
Cách gỡ PowerShell trên Linux?
Hãy học lệnh gỡ PowerShell trên các bản phân phối Linux khác nhau:
- Gỡ PowerShell Core trên Ubuntu/Linux Mint:
sudo apt remove powershell - Gỡ PowerShell trên Debian:
sudo apt remove powershell - Gỡ PowerShell trên Kali Linux:
sudo apt -y remove powershell - Gỡ PowerShell trên CentOS hoặc RHEL:
sudo yum remove powershell - Gỡ PowerShell dùng Snap:
sudo snap remove powershell
Cách chạy và sử dụng PowerShell Core trên Linux?
Để chạy shell lệnh PowerShell trên Linux, chạy lệnh:
pwsh
Bạn có thể kiểm tra phiên bản PowerShell cài trên máy Ubuntu bằng lệnh:
$PSVersionTable
Khi gõ lệnh PowerShell, bạn có thể dùng tính năng tự động hoàn thành mã với phím TAB.
Trong ví dụ, PowerShell Core 7.2.0 được cài:
PSVersion 7.2.0 PSEdition Core OS Linux 5.4.0-81-generic #91-Ubuntu SMP Thu Jul 15 19:09:17 UTC 2021 Platform Unix

Bạn thấy lệnh trong shell PowerShell mất nhiều thời gian hơn so với bash. Bạn có thể so sánh thời gian thực thi:
time pwsh -Command Get-History
time bash -c history

Mặc định, một số module PowerShell tích hợp sẵn có sẵn trên Linux:
Get-Module –ListAvailable
- Microsoft.PowerShell.Archive
- Microsoft.PowerShell.Host
- Microsoft.PowerShell.Management
- Microsoft.PowerShell.Security
- Microsoft.PowerShell.Utility
- PackageManagement
- PowerShellGet
- PSReadLine
- ThreadJob

Bạn có thể cài các module khác, bao gồm VMWare PowerCLI.
Để lấy ngày hiện tại:
Get-date
Kiểm tra uptime máy chủ:
get-Uptime
Để liệt kê nội dung thư mục hiện tại:
Dir
Hoặc:
Get-ChildItem

Lưu ý rằng lệnh PowerShell trên Linux không phân biệt chữ hoa chữ thường.
Để hiển thị lịch sử lệnh PowerShell:
History
Để lấy trợ giúp về lệnh:
Get-help Get-History
Để chạy script PowerShell từ bash:
pwsh /home/sysops/CheckSpace.ps1
Chúng tôi sẽ nói thêm về tính năng và kịch bản sử dụng PowerShell cho quản trị và tự động hóa Linux trong bài sau.