Using Linux Commands in PowerShell: Cross-Platform Workflows with pwsh and WSL
PowerShell and Linux are now far more connected than they used to be. Thanks to modern, cross-platform PowerShell (pwsh) and the Windows Subsystem for Linux (WSL), you can bring Linux commands directly into your PowerShell workflow—whether you’re working from Windows, Linux, or macOS.
Put simply, you can use PowerShell to manage and automate tasks inside a Linux environment running through WSL, or you can install PowerShell directly on a Linux system and use it as your primary shell and scripting language. This combination gives developers and administrators a practical way to blend Windows and Linux tooling and workflows in one place.
In this tutorial, you’ll learn how to unlock Linux functionality inside PowerShell, apply compatibility best practices, and get more value from modern command-line tooling—across the operating system you prefer.
Key Takeaways
- Three methods: native
pwshon Linux, Windows PowerShell with WSL integration, or PowerShell Remoting into a Linux host. - Compatibility layer: PowerShell forwards unknown commands to
/bin/bashvia WSL, or runs native binaries on Linux. - Code parity: around 95% of common Bash one-liners (
grep,awk,sed) work without modification insidepwshwhen$env:PATHincludes GNU coreutils. - Use-cases: cross-platform scripts, DevOps pipelines, and mixed Windows/Linux fleets.
Understanding PowerShell vs. Bash
PowerShell and Bash are both capable command-line shells, but they differ heavily in how they work, how they are scripted, and what they are best at. The comparison below highlights key differences so you can better choose the right tool for the job.
| Feature | Bash (GNU) | PowerShell 7 (pwsh) |
|---|---|---|
| Data Handling | Text streams only (plain strings, lines) | Structured .NET objects (can output as JSON, XML) |
| OS Support | Linux, macOS, WSL, some Windows (via Cygwin) | Windows, Linux, macOS, WSL (fully cross-platform) |
| Package Manager | apt, dnf, yum, pacman, brew | winget, choco, apt, plus Install-Module for PowerShell Gallery |
| Pipeline | Passes text via \n (stdout/stdin) | Passes rich objects between commands (object pipeline) |
| Scripting Syntax | POSIX shell syntax, terse, symbolic | Verb-Noun cmdlets, .NET-based, more verbose |
| Extensibility | Shell scripts, external binaries, aliases | Cmdlets, modules, classes, external binaries |
| Remote Execution | SSH, scp, rsync, expect | PowerShell Remoting (WinRM, SSH), Invoke-Command |
| Tab Completion | Bash completion, programmable | Advanced, context-aware, works with objects |
| Error Handling | Exit codes, set -e, traps | Try/Catch/Finally, structured exceptions |
| Interactivity | Readline, history, job control | PSReadLine, history, background jobs, transcripts |
| Integration | Deep integration with Unix tools | Deep integration with Windows, .NET, and REST APIs |
| Default Location | /bin/bash, /usr/bin/bash | pwsh (cross-platform), powershell.exe (Windows) |
PowerShell is a strong choice for automation, administration, and tasks that benefit from structured data handling—especially when working in Windows-heavy or mixed environments.
Bash shines for fast scripting, Linux/Unix system management, and chaining traditional command-line tools together.
PowerShell can execute Bash commands (particularly through WSL), but Bash cannot natively handle PowerShell objects.
Because both shells are now available across platforms, developers and sysadmins can build workflows that remain flexible and portable.
For most DevOps and automation work, choose the shell that best aligns with your environment, scripting style, and tooling integrations.
Takeaway: PowerShell can host Bash; Bash cannot natively interpret PowerShell objects.
Method 1 – Install PowerShell on Linux
On Ubuntu and Debian-based Linux distributions, you can install PowerShell using the commands below.
For Debian/Ubuntu-based systems, install prerequisites:
# Install necessary packages for adding Microsoft repository and installing PowerShell
sudo apt-get install -y wget apt-transport-https software-properties-common
# Add Microsoft repository and key
sudo apt update && \
sudo apt install -y wget gnupg && \
# Download and add Microsoft's GPG key to the trusted keys list
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc && \
# Add the Microsoft repository for PowerShell
sudo add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://packages.microsoft.com/ubuntu/22.04/prod jammy main"
# Update package list and install PowerShell
sudo apt update && sudo apt install -y powershell
# Launch PowerShell
pwsh
# Run a Linux binary to verify the environment
uname -a
Output
Linux penguin 6.5.0-23-generic #24-Ubuntu ...
Method 2 – Use Windows Subsystem for Linux (WSL)
On Windows, you can use the Windows Subsystem for Linux (WSL) to run Linux commands directly from PowerShell.
On Windows:
# Enable WSL & install Ubuntu-24.04 distro
wsl --install -d Ubuntu-24.04
PowerShell ↔ Linux command pass-through:
PS C:\> wsl ls -lah /home
-rw-r--r-- 1 root root 0 Jul 1 ...
PS C:\> wsl cat /etc/os-release | sls VERSION
VERSION="22.04.4 LTS (Jammy Jellyfish)"
To install PowerShell (pwsh) inside Ubuntu WSL, run:
sudo apt update && sudo apt install -y wget apt-transport-https software-properties-common
wget -q "https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb"
sudo dpkg -i packages-microsoft-prod.deb
sudo apt update
sudo apt install -y powershell
Now you can run PowerShell commands inside Ubuntu WSL:
PS /home/ubuntu> pwsh
Note: You can use GPT-4o prompt example to automate WSL installs: “Write a PowerShell script that installs WSL 2, sets Ubuntu as default, then creates a bash alias within .bashrc.”
Method 3 – Cross-Platform Scripts & Aliases
You can also use PowerShell Remoting to execute Linux commands from Windows PowerShell.
# Connect to a Linux host
Enter-PSSession -ComputerName <linux-host>
# Run Linux commands
ls -l /home
# Exit the session
Create mixed.ps1 with:
# Call native Linux grep from PowerShell
wsl grep -R "ERROR" /var/log/syslog | Select-String -Pattern 'auth'
# Pipe PS object to bash via JSON
Get-Process | ConvertTo-Json | wsl jq '.[] | select(.CPU > 1)'
Run from Windows or Linux host.
Alias Bash cmd in PowerShell profile ($HOME/.config/powershell/Microsoft.PowerShell_profile.ps1):
Set-Alias grep wsl grep
Set-Alias jq wsl jq
# Run from Windows or Linux host
.\mixed.ps1
Translating Common Bash Tasks to PowerShell
When working across Linux (Bash) and Windows (PowerShell), many administrative and scripting tasks are similar in purpose, but they differ in syntax and in which commands are available.
Below is a quick reference table showing how to handle common file and system operations in both environments. This can help you adjust scripts or commands when switching between Bash and PowerShell, especially in hybrid or WSL (Windows Subsystem for Linux) setups.
| Task | Bash Command | PowerShell Command |
|---|---|---|
| Find large files | du -ah . | sort -h | tail -n 20 |
Get-ChildItem -Recurse | Sort-Object Length -Descending | Select-Object -First 20 |
| Replace text in files | sed -i 's/foo/bar/g' *.txt |
Get-ChildItem *.txt | ForEach-Object { (Get-Content $_) -replace 'foo','bar' | Set-Content $_ } |
| Update packages (Deb) | sudo apt update && sudo apt upgrade |
sudo apt update; sudo apt upgrade (run inside WSL or from a remote session) |
The Bash commands are generally executed on Linux systems or inside WSL on Windows.
The PowerShell alternatives are intended for Windows, but many of them can also be used in PowerShell Core on Linux or through remoting.
For package management, PowerShell itself does not manage Linux packages, but you can run Bash commands from PowerShell when using WSL or remote sessions.
This table is only a starting point—many additional tasks can be translated in the same way. When adapting scripts, always verify command-line differences and test them in the environment you’re targeting.
What are the Pros and Cons of Using Linux with PowerShell?
| Pros | Cons |
|---|---|
| Cross-Platform Compatibility: PowerShell runs natively on Windows, Linux, and macOS, whereas Bash is primarily Linux-based. | Learning Curve: PowerShell’s syntax and object-oriented approach can feel unfamiliar to users used to Bash scripting. |
| PowerShell Remoting: Lets you execute Linux commands from Windows PowerShell, enabling remote management across platforms. | Feature Gaps: Some native Linux utilities or scripts may not behave the same in PowerShell and may need adjustments or workarounds. |
| WSL Integration: Allows Linux commands to run smoothly inside PowerShell using Windows Subsystem for Linux, connecting both environments. | Performance Overhead: Running Linux commands through WSL or remoting can add latency compared to executing them directly in Bash. |
| PowerShell Core: Available on all major platforms, allowing direct execution of many Linux commands and scripts. | Ecosystem Differences: Not every PowerShell module is available or fully supported on Linux, which may reduce functionality. |
| Performance Insights: PowerShell pipelines can efficiently process large objects and structured data, often reducing the need for extra external tools. | Resource Usage: PowerShell can use more CPU and memory than lightweight Bash scripts, especially for simple or repetitive tasks. |
| Script Portability: PowerShell scripts can be more portable across operating systems due to consistent cmdlet behavior, reducing OS-specific logic. | Startup Time: PowerShell typically starts slower than Bash, which can matter for short-lived scripts or frequent runs. |
What are the Common Mistakes & Fixes When Using Linux with PowerShell?
When combining Linux tooling with PowerShell, a few recurring issues tend to appear—especially around remoting, WSL command execution, and cross-platform script behavior. The table below outlines frequent mistakes and the most practical fixes.
| Common Error | How to Troubleshoot |
|---|---|
PowerShell Remoting: Attempting to use Enter-PSSession to connect to a Linux host. |
If you run into connection issues, use ssh instead of Enter-PSSession for Linux systems, since Enter-PSSession is mainly intended for Windows remoting. |
| WSL Integration: Linux commands fail or are not recognized in PowerShell. | Make sure you prefix Linux commands with wsl (for example, wsl ls). If it still does not work, confirm WSL is installed and available in your environment. |
| PowerShell Core: PowerShell commands not found or incompatible on Linux/macOS. | Start PowerShell Core using pwsh, which is designed to work cross-platform. If pwsh is missing, install PowerShell Core for your operating system. |
| Script Portability: Scripts behave differently across platforms. | Look for OS-specific cmdlets or paths. Use consistent, cross-platform cmdlets and validate scripts on every target platform. |
| Startup Time: PowerShell starts slowly, especially on Linux. | If startup feels slow, make sure you’re running the latest PowerShell Core (pwsh). Remove or disable unnecessary modules in your profile to speed up launch time. |
FAQs
1. Can PowerShell run Linux commands?
Yes, PowerShell can execute Linux commands, but the approach depends on your environment:
On Windows with WSL (Windows Subsystem for Linux)
You can run Linux commands from PowerShell by adding the wsl prefix. For example:
wsl ls -la /home
wsl grep "pattern" file.txt
This runs the command inside your default WSL distribution and returns the output to PowerShell.
On Linux or macOS (with PowerShell Core)
PowerShell Core (pwsh) runs natively on Linux and macOS. You can execute native Linux commands directly, similar to how you would in Bash:
ls -la /var/log
grep "error" /var/log/syslog
PowerShell will pass these commands to the underlying shell.
From PowerShell scripts
You can use the Invoke-Expression cmdlet or call commands directly:
Invoke-Expression "ls -l /tmp"
Note: On Windows without WSL, native Linux commands are not available unless you use a compatibility layer (like Cygwin) or WSL. Some Linux commands may behave differently or may not be available, depending on your environment.
2. Is PowerShell available on Linux?
Yes, PowerShell Core (now simply called “PowerShell”) is fully supported on Linux.
Installation
You can install PowerShell on most major Linux distributions. For example, on Ubuntu:
# Install prerequisites
sudo apt-get update
sudo apt-get install -y wget apt-transport-https software-properties-common
# Import the Microsoft repository
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
sudo dpkg -i packages-microsoft-prod.deb
# Install PowerShell
sudo apt-get update
sudo apt-get install -y powershell
# Start PowerShell
pwsh
Usage
Once installed, start PowerShell by typing pwsh in your terminal.
Features
Most core PowerShell features and modules work on Linux, but some Windows-specific modules (like those for managing Windows services or the registry) are not available.
3. Can I use Bash and PowerShell together?
Yes, Bash and PowerShell can be combined in multiple ways:
From PowerShell, call Bash commands
On Windows with WSL:
wsl echo "Hello from Bash"
wsl uname -a
On Linux/macOS, just call Bash commands directly:
bash -c "echo Hello from Bash"
From Bash, call PowerShell scripts
If PowerShell (pwsh) is installed, you can run PowerShell scripts directly from Bash:
pwsh -Command "Get-Process | Where-Object { \$_.CPU -gt 100 }"
Hybrid scripts
You can build scripts that mix Bash and PowerShell by invoking one from the other as needed. For instance, a Bash script can call a PowerShell script for Windows-specific tasks, and a PowerShell script can invoke Bash commands for Linux automation.
Interoperability Example
# PowerShell script calling a Bash command
$result = wsl whoami
Write-Output "Current WSL user: $result"
# Bash script calling a PowerShell command
pwsh -Command "Get-Date"
Tip: When mixing scripts, be mindful of environment variables, path formats, and output encoding, as these can differ between shells.
4. Do I need WSL to use Linux in PowerShell?
You only need WSL (Windows Subsystem for Linux) if you want to run native Linux binaries and commands from PowerShell on Windows.
On Windows
With WSL: You can run Linux commands and scripts directly from PowerShell using the wsl prefix.
Without WSL: PowerShell cannot run native Linux commands unless you use another compatibility layer (like Cygwin or Git Bash), but these are not as seamless as WSL.
On Linux/macOS
PowerShell Core (pwsh) runs natively, and you can execute Linux commands directly—no WSL required.
Example (Windows with WSL)
# PowerShell script calling a Bash command
$result = wsl whoami
Write-Output "Current WSL user: $result"
Conclusion
In this tutorial, you learned how to connect Bash and PowerShell so both shells can work together across Linux and Windows command-line environments. This included running PowerShell commands from Bash and running Bash commands from PowerShell, creating hybrid scripts that take advantage of both shells for cross-platform automation, and practical examples of calling Bash from PowerShell and PowerShell from Bash.
In addition, you explored important details around environment variables, path formatting, and output encoding when mixing shells. You also examined how WSL (Windows Subsystem for Linux) enables Linux command execution on Windows, and how PowerShell Core makes Linux command execution native on Linux and macOS. By applying these techniques, you can simplify your workflow, automate complex cross-platform tasks, and fully leverage both shell environments.


