DEV Community

kazeem mohammed
kazeem mohammed

Posted on

PowerShell Programming and Scripting: A Complete Guide

PowerShell has evolved from a simple command-line shell into a powerful automation and scripting platform for Windows, Linux, and macOS. Whether you’re managing infrastructure, automating repetitive tasks, or building complex CI/CD pipelines, PowerShell offers the flexibility of scripting combined with the power of the .NET framework.

1. What is PowerShell?

PowerShell is a task automation and configuration management framework from Microsoft, consisting of:

  • Command-line shell : An interactive interface to run commands (cmdlets).
  • Scripting language : Based on the .NET framework, offering full programming constructs.
  • Configuration management : Via Desired State Configuration (DSC).

Originally released in 2006, PowerShell is now open-source and cross-platform, with PowerShell Core (from version 6 onwards) running on Windows, macOS, and Linux.

References:

2. Why Use PowerShell?

  1. Automation  — Simplifies repetitive administrative tasks.
  2. Cross-platform  — Works on Windows, macOS, and Linux.
  3. Integration with .NET  — Access full .NET libraries.
  4. Pipeline support  — Pass objects between commands.
  5. Remoting  — Manage remote systems easily.

3. PowerShell Basics

Cmdlets

Cmdlets are built-in PowerShell commands. They follow a Verb-Noun naming convention, e.g., Get-Process, Set-ExecutionPolicy.

# List running processes
Get-Process

# Get system services
Get-Service
Enter fullscreen mode Exit fullscreen mode

Variables

PowerShell variables start with a $ symbol.

$Name = "Mate"
$Age = 32
Write-Output "Name: $Name, Age: $Age"
Enter fullscreen mode Exit fullscreen mode

Pipelines

Unlike other shells, PowerShell passes objects between commands, not just text.

Get-Process | Where-Object {$_.CPU -gt 100}
Enter fullscreen mode Exit fullscreen mode

4. Scripting with PowerShell

A PowerShell script is simply a .ps1 file containing commands.

Example: Hello.ps1

param(
    [string]$UserName = "World"
)

Write-Output "Hello, $UserName!"
Enter fullscreen mode Exit fullscreen mode

Run it:

.\Hello.ps1 -UserName "Mate"
Enter fullscreen mode Exit fullscreen mode

Conditional Statements

$score = 85

if ($score -ge 90) {
    "Grade: A"
} elseif ($score -ge 75) {
    "Grade: B"
} else {
    "Grade: C"
}
Enter fullscreen mode Exit fullscreen mode

Loops

foreach ($i in 1..5) {
    Write-Output "Number: $i"
}
Enter fullscreen mode Exit fullscreen mode

5. Advanced Features

Functions

function Get-Square {
    param([int]$Number)
    return $Number * $Number
}

Get-Square -Number 5
Enter fullscreen mode Exit fullscreen mode

Error Handling

try {
    Get-Item "C:\NonExistentFile.txt" -ErrorAction Stop
} catch {
    Write-Output "An error occurred: $_"
}
Enter fullscreen mode Exit fullscreen mode

Modules

Modules extend PowerShell functionality.

# Install a module
Install-Module -Name Az -Scope CurrentUser

# Import a module
Import-Module Az
Enter fullscreen mode Exit fullscreen mode

Remoting

# Enable remoting (run as admin)
Enable-PSRemoting -Force

# Execute command on remote computer
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process }
Enter fullscreen mode Exit fullscreen mode

6. Real-World Examples

Example 1: Bulk User Creation in Active Directory

Import-Csv "users.csv" | ForEach-Object {
    New-ADUser -Name $_.Name -SamAccountName $_.Username -AccountPassword (ConvertTo-SecureString $_.Password -AsPlainText -Force) -Enabled $true
}
Enter fullscreen mode Exit fullscreen mode

Example 2: Monitoring Disk Space

Get-PSDrive -PSProvider FileSystem | Where-Object {$_.Free -lt 10GB}
Enter fullscreen mode Exit fullscreen mode

Example 3: Automating Azure Resource Creation

Connect-AzAccount
New-AzResourceGroup -Name "MyRG" -Location "EastUS"
Enter fullscreen mode Exit fullscreen mode

7. Best Practices

  1. Use Verb-Noun naming for functions.
  2. Comment your code with #.
  3. Error handling using try { } catch { }.
  4. Avoid hardcoded credentials  — use Get-Credential or secure vaults.
  5. Modularize scripts for reusability.

8. Learning Resources

  • PowerShell Documentation — Microsoft
  • PowerShell Gallery
  • PowerShell.org
  • Book: Learn Windows PowerShell in a Month of Lunches by Don Jones and Jeffrey Hicks.

Conclusion

PowerShell is more than just a scripting language — it’s a full-fledged automation framework that can integrate with Windows, Linux, cloud services, and enterprise tools. Whether you are a system administrator, DevOps engineer, or cloud architect, mastering PowerShell will save you countless hours and open up opportunities for advanced automation.

References:

Top comments (1)

Collapse
 
kazeem_mohammed profile image
kazeem mohammed

Thanks for reading! I’d love to hear your thoughts—please share them in the comments