Translate
From bash to PowerShell
You know ls, grep, and tail -f. Here is what to type in PowerShell instead. Copy the right-hand column. Also see From cmd.
| bash | PowerShell | Learn more |
|---|---|---|
echo 'Another line' >> notes.txt |
Add-Content -Path .\notes.txt -Value 'Another line' |
Add-Content |
curl -s https://api.example.com/items | jq |
Invoke-RestMethod -Uri 'https://api.example.com/items' |
Invoke-RestMethod |
cd docs |
Set-Location -Path .\docs |
Set-Location |
sha256sum installer.exe |
Get-FileHash -Path .\installer.exe -Algorithm SHA256 |
Get-FileHash |
df -h |
Get-Volume | Select-Object DriveLetter, FileSystemLabel, SizeRemaining, Size |
Get-Volume |
test -e docs && echo yes |
Test-Path -Path .\docs |
Test-Path |
clear |
Clear-Host |
Clear-Host |
diff a.txt b.txt |
Compare-Object (Get-Content -Path .\a.txt) (Get-Content -Path .\b.txt) |
Compare-Object |
cp notes.txt backup/ |
Copy-Item -Path .\notes.txt -Destination .\backup\notes.txt |
Copy-Item |
ls | pbcopy |
Get-ChildItem -Name | Set-Clipboard |
Set-Clipboard |
wc -l notes.txt |
(Get-Content -Path .\notes.txt | Measure-Object -Line).Lines |
Measure-Object |
mkdir -p docs |
New-Item -Path .\docs -ItemType Directory |
New-Item |
rm -rf temp/ |
Remove-Item -Path .\temp -Recurse -Force -WhatIf |
Remove-Item |
curl -O https://example.com/file.zip |
Invoke-WebRequest -Uri 'https://example.com/file.zip' -OutFile .\file.zip |
Invoke-WebRequest |
ls | grep '\.md$' |
Get-ChildItem | Where-Object { $_.Extension -eq '.md' } |
Where-Object |
which git |
Get-Command git |
Get-Command |
sudo lsof -i :8080 |
Get-NetTCPConnection -LocalPort 8080 | Select-Object LocalAddress, LocalPort, State, OwningProcess |
Get-NetTCPConnection |
date '+%Y-%m-%d %H:%M:%S' |
Get-Date -Format 'yyyy-MM-dd HH:mm:ss' |
Get-Date |
alias |
Get-Alias |
Get-Alias |
df -h |
Get-PSDrive |
Get-PSDrive |
ls -la |
Get-ChildItem -Force |
Get-ChildItem |
ip addr |
Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress |
Get-NetIPAddress |
ps aux --sort=-%cpu | head |
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10 |
Get-Process |
systemctl list-units --type=service --state=running |
Get-Service | Where-Object Status -eq 'Running' |
Get-Service |
dig example.com |
Resolve-DnsName -Name example.com |
Resolve-DnsName |
mv notes.txt archive/ |
Move-Item -Path .\notes.txt -Destination .\archive\notes.txt |
Move-Item |
xdg-open notes.txt |
Start-Process -FilePath .\notes.txt |
Start-Process |
echo 'Hello' > notes.txt |
Set-Content -Path .\notes.txt -Value 'Hello' |
Set-Content |
jq . data.json |
Get-Content -Path .\data.json -Raw | ConvertFrom-Json |
ConvertFrom-Json |
sleep 5 |
Start-Sleep -Seconds 5 |
Start-Sleep |
ls | head -5 |
Get-ChildItem | Select-Object -First 5 |
Select-Object |
ping -c 2 1.1.1.1 |
Test-Connection -ComputerName 1.1.1.1 -Count 2 |
Test-Connection |
echo 'Backup finished' |
Write-Output 'Backup finished' |
Write-Output |
dirname docs/notes.txt |
Split-Path -Path .\docs\notes.txt -Parent |
Split-Path |
cat notes.txt |
Get-Content -Path .\notes.txt |
Get-Content |
man ls |
Get-Help Get-ChildItem -Examples |
Get-Help |
journalctl -p err -n 20 |
Get-WinEvent -LogName System -MaxEvents 20 |
Get-WinEvent |
pbpaste |
Get-Clipboard |
Get-Clipboard |
script session.log |
Start-Transcript -Path (Join-Path $env:TEMP 'ps-transcript.txt') |
Start-Transcript |
mv draft.txt final.txt |
Rename-Item -Path .\draft.txt -NewName final.txt |
Rename-Item |
sudo systemctl restart nginx |
Restart-Service -Name Spooler -WhatIf |
Restart-Service |
sudo reboot |
Restart-Computer -WhatIf |
Restart-Computer |
for f in *; do echo "$f"; done |
Get-ChildItem -File | ForEach-Object { $_.Name } |
ForEach-Object |
ssh pc01 'systemctl status cups' |
Invoke-Command -HostName pc01 -ScriptBlock { systemctl status cups } |
Invoke-Command |
grep -rn 'TODO' docs/ |
Get-ChildItem -Path .\docs -Recurse -File | Select-String -Pattern 'TODO' |
Select-String |
history 20 |
Get-History -Count 20 |
Get-History |
ls -ld docs |
(Get-Acl -Path .\docs).Access | Select-Object IdentityReference, FileSystemRights, AccessControlType |
Get-Acl |
ls > listing.txt |
Get-ChildItem | Out-File -FilePath .\listing.txt |
Out-File |
pwd |
Get-Location |
Get-Location |
ls -S |
Get-ChildItem | Sort-Object -Property Length -Descending |
Sort-Object |
sudo systemctl start nginx |
Start-Service -Name Spooler |
Start-Service |
kill 1234 |
Stop-Process -Id 1234 -WhatIf |
Stop-Process |
sudo systemctl stop nginx |
Stop-Service -Name Spooler -WhatIf |
Stop-Service |
uname -a |
Get-ComputerInfo | Select-Object CsName, WindowsVersion, OsArchitecture |
Get-ComputerInfo |
nc -zv example.com 443 |
Test-NetConnection -ComputerName example.com -Port 443 |
Test-NetConnection |
time ls -R docs |
Measure-Command { Get-ChildItem -Path .\docs -Recurse } |
Measure-Command |
realpath docs |
Resolve-Path -Path .\docs |
Resolve-Path |
unzip docs.zip -d docs-unzipped |
Expand-Archive -Path .\docs.zip -DestinationPath .\docs-unzipped |
Expand-Archive |
zip -r docs.zip docs |
Compress-Archive -Path .\docs -DestinationPath .\docs.zip |
Compress-Archive |
Gotchas for bash users
- Output is objects, not text.
Get-ChildItem | Select-Object Name, Lengthpicks properties. You rarely needawkorcut. See Pipelines. ls -ladoes not work.lsis an alias forGet-ChildItem, which has its own parameters. UseGet-ChildItem -Forceto include hidden files.rm -rfbecomesRemove-Item -Recurse -Force. Add-WhatIffirst. There is no recycle bin.curlandwgetare aliases on Windows PowerShell 5.1 forInvoke-WebRequest, so flags like-Ofail. PowerShell 7 removed those aliases, socurlruns the real curl.exe.- Comparison uses words.
-eq,-ne,-gt,-lt. A bare>is redirection and will create a file. See Operators. - Environment variables are
$env:PATH, not$PATH. Set one with$env:NAME = 'value'. &&and||only work in PowerShell 7. In 5.1, use;orif ($?) { ... }.- Case-insensitive by default.
-eq,-like, andSelect-Stringignore case. Use-ceq,-clike, or-CaseSensitivewhen it matters. - Strings: double quotes expand
$variables, single quotes do not. The escape character is the backtick`, not the backslash.