Skip to main content

Command Line Interfaces

The command line is one of the most powerful tools for system administrators. While graphical interfaces make many tasks easier, the command line provides:
  • More direct system communication
  • Better automation capabilities
  • Access to all system features
  • Scriptability for repetitive tasks

PowerShell (Windows)

PowerShell has become the default interpreter in Windows 10 (since November 2017), replacing CMD as the primary administrative tool.

Why PowerShell?

Object-Oriented

Everything is an object, not text strings, providing richer data manipulation

.NET Integration

Full access to .NET Framework capabilities

Cross-Platform

PowerShell Core runs on Windows, Linux, and macOS

Consistent Syntax

Verb-Noun naming convention makes commands predictable

cmdlet Structure

All PowerShell cmdlets follow the Verb-Noun pattern:
Get-Process      # Get information
Set-ExecutionPolicy  # Set configuration
New-Item         # Create new item
Remove-Item      # Delete item

Common Verbs

VerbPurposeExample
GetRetrieve dataGet-Process
SetModify settingsSet-Location
NewCreate objectsNew-Item
RemoveDelete objectsRemove-Item
EnableActivate featureEnable-WindowsFeature
DisableDeactivate featureDisable-FirewallRule
InstallInstall softwareInstall-Module
UninstallRemove softwareUninstall-Package
TestVerify conditionTest-Path
MoveRelocate objectMove-Item

Parameters

Parameters modify command behavior:
Get-ChildItem -Path C:\ -Recurse -Force
Get-Process -Name "chrome" | Stop-Process

Pipelines

PowerShell’s pipelines pass entire objects between commands:
# Get processes, sort by ID, select first 5
Get-Process | Sort-Object -Property Id -Descending | Select-Object -First 5

# Get services, filter running ones, export to CSV
Get-Service | Where-Object {$_.Status -eq "Running"} | Export-Csv services.csv

Script Files (.ps1)

PowerShell scripts use the .ps1 extension:
#!/usr/bin/env pwsh
# My first PowerShell script

Write-Host "System Information"
Get-ComputerInfo | Select-Object CsName, OsVersion

Help System

PowerShell includes comprehensive help:
Get-Help Get-Process

Execution Policy

PowerShell’s execution policy controls script execution:
# View current policy
Get-ExecutionPolicy

# Set unrestricted (for development)
Set-ExecutionPolicy -ExecutionPolicy Unrestricted

# Set to remote signed (production)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Changing execution policy requires administrator privileges. Run PowerShell as administrator.
Execution Policy Levels:
PolicyDescription
RestrictedNo scripts can run (default)
AllSignedOnly signed scripts can run
RemoteSignedLocal scripts run unsigned; downloaded scripts must be signed
UnrestrictedAll scripts can run (prompts for downloaded scripts)
BypassNo restrictions, no warnings

BASH Shell (GNU/Linux)

BASH (Bourne Again SHell) is the default shell on most Linux distributions.

The Prompt

The prompt provides information about your session:
violin@xubuntu:~$
  • violin - username
  • xubuntu - hostname
  • ~ - current directory (home)
  • $ - regular user (# for root)
Root prompt:
root@xubuntu:~#

Key Features

Press TAB to auto-complete:
  • Commands
  • File names
  • Directory names
  • Variables
Press TAB twice to see all possibilities:
cd /us<TAB><TAB>
# Shows: /usr/ /usr/bin/ /usr/lib/ ...
Navigate command history with arrow keys:
  • - Previous command
  • - Next command
  • history - Show command history
  • !123 - Execute command #123 from history
  • !! - Execute previous command
  • !$ - Last argument of previous command
History is stored in ~/.bash_history (default 1000 commands)
Create command shortcuts:
alias ll='ls -lah'
alias update='sudo apt update && sudo apt upgrade'
alias ..='cd ..'
Make permanent by adding to ~/.bashrc
Define reusable functions:
mkcd() {
    mkdir -p "$1" && cd "$1"
}

backup() {
    cp "$1" "$1.backup.$(date +%Y%m%d)"
}

Script Structure

BASH scripts follow a standard structure:
#!/bin/bash
# Script description
# Author: Your Name
# Date: 2024-03-06

# Variables
VARIABLE="value"

# Main code
echo "Script starting..."

# Functions
function my_function() {
    echo "Function called"
}

# Execution
my_function

# Exit with success
exit 0

Shebang Line

The shebang (#!) specifies the interpreter:
#!/bin/bash          # Use bash
#!/bin/sh            # Use sh (POSIX)
#!/usr/bin/env bash  # Use bash from PATH
#!/usr/bin/env python3  # Python script
Using #!/usr/bin/env bash is more portable as it finds bash in the PATH.

Making Scripts Executable

# Create script
echo '#!/bin/bash' > script.sh
echo 'echo "Hello World"' >> script.sh

# Make executable
chmod +x script.sh

# Run script
./script.sh

Common BASH Constructs

#!/bin/bash

if [ -f "/etc/passwd" ]; then
    echo "File exists"
else
    echo "File not found"
fi

Comparing PowerShell and BASH

TaskPowerShellBASH
List filesGet-ChildItemls
Change directorySet-Locationcd
Copy fileCopy-Itemcp
Move fileMove-Itemmv
Delete fileRemove-Itemrm
Create directoryNew-Item -Type Directorymkdir
Display textWrite-Hostecho
View fileGet-Contentcat
Find textSelect-Stringgrep
Sort outputSort-Objectsort
Filter outputWhere-Objectgrep
Both shells support aliases. PowerShell includes aliases that match common BASH commands (ls, cd, cat, etc.).

Best Practices

1

Document Your Scripts

Include comments explaining what the script does, author, and date
2

Use Meaningful Names

Variables and functions should have descriptive names
3

Check for Errors

Always verify commands succeeded before continuing
4

Test Before Production

Test scripts thoroughly in a development environment
5

Use Version Control

Store scripts in git for history and collaboration

Build docs developers (and LLMs) love