In the world of task automation, mastering the command line is an essential skill. Whether using traditional GNU commands or navigating the Windows ecosystem with the powerful PowerShell, knowing the right tools can transform your productivity.
In this post, weβll compare 50 GNU commands with their PowerShell equivalents.
rm -rf /home/$USER/folder
Remove-Item -Path "C:\folder" -Recurse -Force
ps aux | grep apache2 # httpd
systemd:
systemctl status apache2
Get-Service | Where-Object { $_.DisplayName -like "*Apache*" }
sudo kill -9 $(pidof apache2) # httpd
systemd:
sudo systemctl stop apache2
Stop-Service -Name "Apache2.4"
unset VARIABLE_NAME
C:\App\bin
# Get the current value of the system Path environment variable
$envPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
# Split paths into an array
$paths = $envPath -split ';'
# Filter to remove the unwanted path
$newPaths = $paths | Where-Object { $_ -ne "C:\App\bin" }
# Rebuild the Path environment variable (without the unwanted path)
$newPathString = ($newPaths -join ';').TrimEnd(';')
# Update the system environment variable
[Environment]::SetEnvironmentVariable("Path", $newPathString, [EnvironmentVariableTarget]::Machine)
which mycommand
Get-Command mycommand
mkdir my-project
New-Item -ItemType Directory "MyProject"
mkdir -p my-project/folder/new
New-Item -Path "C:/MyProject/folder/new" -ItemType Directory
mv folder new/path/
Move-Item -Path "folder" -Destination "C:\new\path\"
cd folder/
Set-Location folder
cp file path/to/dest
cp -r folder/ path/to/dest
Copy-Item file path\to\dest
Copy-Item folder\ -Recurse -Destination path\to\dest
$HOME
# echo $HOME
$USER
# echo $USER
$env:USERPROFILE
# Write-Host $env:USERPROFILE
$env:USERNAME
# Write-Host $env:USERNAME
ls -la
Get-ChildItem -Force
cat file.txt
Get-Content file.txt
grep "term" file.txt
Select-String -Pattern "term" -Path file.txt
df -h
Get-PSDrive -PSProvider FileSystem
free -h
Get-CimInstance Win32_OperatingSystem | Select-Object TotalVisibleMemorySize,FreePhysicalMemory
printenv
Get-ChildItem Env:
mv oldname newname
Rename-Item -Path oldname -NewName newname
sudo command
Start-Process powershell -Verb runAs
ip addr show
Get-NetIPAddress
Example for Terlang:
C:\Program Files\Terlang\bin
(Windows) and${HOME}/.local/terlang/bin/
(GNU)
export PATH="${PATH}:${HOME}/.local/terlang/bin/"
[System.Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Program Files\Terlang\bin", [System.EnvironmentVariableTarget]::Machine)
tail -n 20 file.log
Get-Content file.log -Tail 20
top
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10
(not real-time, but shows a snapshot of top CPU-consuming processes)
pkill -f process
Get-Process -Name process | Stop-Process -Force
tail -f file.log
Get-Content file.log -Wait
tar -czvf archive.tar.gz folder/
Compress-Archive -Path folder\* -DestinationPath archive.zip
unzip archive.zip
Expand-Archive -Path archive.zip -DestinationPath destination\
echo $VARIABLE
$env:VARIABLE
export VARIABLE=value
$env:VARIABLE="value"
uname -a
Get-CimInstance Win32_OperatingSystem | Select-Object Caption, Version, OSArchitecture
date
Get-Date
who
query user
sudo netstat -tulpn
Get-NetTCPConnection | Select-Object LocalAddress,LocalPort,OwningProcess
find /path -name "file.txt"
Get-ChildItem -Path C:\path -Recurse -Filter "file.txt"
crontab -e
# Simple example to create a scheduled task via PowerShell
$action = New-ScheduledTaskAction -Execute "notepad.exe"
$trigger = New-ScheduledTaskTrigger -At 9am -Daily
Register-ScheduledTask -TaskName "OpenNotepad" -Action $action -Trigger $trigger
clear
Clear-Host
env
Get-ChildItem Env:
diff file1 file2
Compare-Object (Get-Content file1) (Get-Content file2)
./script.sh
.\script.ps1
Ctrl + C
Ctrl + C
history
Get-History
cat ~/.bash_history
Get-Content (Get-PSReadlineOption).HistorySavePath
history | grep term
Get-History | Where-Object CommandLine -Match "term"
set
Get-Variable
VARIABLE=value
$VARIABLE = "value"
command | less
command | Out-Host -Paging
alias ll='ls -la'
Set-Alias ll Get-ChildItem
unalias ll
Remove-Item Alias:ll
lscpu
Get-CimInstance Win32_Processor | Select-Object Name,NumberOfCores,NumberOfLogicalProcessors
vim file.txt
notepad file.txt
Download a File:
wget https://url.com/file.zip
# Or: wget https://url.com/file.zip -O newname.zip
Invoke-WebRequest -Uri "https://url.com/file.zip" -OutFile "file.zip"