Background
HOME / IT / POWERSHELL-101

PowerShell 101: Getting Started with Windows Automation

A beginner's guide to PowerShell, Microsoft's powerful command-line shell and scripting language for Windows automation and administration

rnrran's profile picture
rnrran
Jan 27, 202525 min read
rnrran's avatar

Written by

rnrran

Feature added soon

PowerShell 101: Getting Started with Windows Automation

Introduction

PowerShell is Microsoft's task automation and configuration management framework, consisting of a command-line shell and associated scripting language. Built on the .NET Framework, PowerShell provides powerful capabilities for system administration, automation, and scripting on Windows systems.

This guide will introduce you to PowerShell fundamentals, covering basic commands, scripting concepts, and practical examples to get you started with Windows automation.

PowerShell Logo

What is PowerShell?

PowerShell is:

  • A Command-Line Shell: Interactive command-line interface
  • A Scripting Language: Write scripts to automate tasks
  • An Automation Platform: Manage Windows systems and applications
  • Object-Oriented: Works with .NET objects, not just text

PowerShell Versions

  • Windows PowerShell 5.1: Built into Windows 10/11 and Windows Server
  • PowerShell 7+ (PowerShell Core): Cross-platform, open-source version
  • PowerShell ISE: Integrated Scripting Environment (legacy)
  • VS Code: Modern editor with PowerShell extension (recommended)

Getting Started

Launching PowerShell

Windows 10/11:

  • Press Win + X and select "Windows PowerShell" or "Terminal"
  • Search for "PowerShell" in Start menu
  • Right-click Start button → Windows PowerShell

PowerShell 7+:

PowerShell Profiles

Your PowerShell profile is a script that runs when PowerShell starts. Customize it for your needs:

# View profile path
$PROFILE

# Check if profile exists
Test-Path $PROFILE

# Create profile
New-Item -Path $PROFILE -Type File -Force

# Edit profile
notepad $PROFILE

Basic PowerShell Concepts

Cmdlets (Command-Lets)

PowerShell commands are called cmdlets. They follow a Verb-Noun naming convention:

Get-Process          # Get processes
Set-Location         # Change directory
New-Item             # Create new item
Remove-Item         # Delete item

Aliases

PowerShell provides aliases for common commands:

# Common aliases
ls          # Alias for Get-ChildItem
cd          # Alias for Set-Location
pwd         # Alias for Get-Location
cat         # Alias for Get-Content

Objects, Not Text

Unlike traditional shells, PowerShell works with objects:

# Get process object
$process = Get-Process notepad

# Access object properties
$process.Name
$process.Id
$process.CPU

# Get all properties
$process | Get-Member

Essential Cmdlets

File and Directory Operations

# Navigate directories
Get-Location              # Current directory
Set-Location C:\Users     # Change directory
Push-Location             # Save current location
Pop-Location              # Return to saved location

# List files and folders
Get-ChildItem             # List items
Get-ChildItem -Recurse    # Recursive listing
Get-ChildItem -Filter *.txt

# Create items
New-Item -ItemType Directory -Path "C:\MyFolder"
New-Item -ItemType File -Path "C:\MyFile.txt"

# Read file content
Get-Content "C:\file.txt"
Get-Content "C:\file.txt" -TotalCount 10  # First 10 lines

# Write to file
"Hello World" | Out-File "C:\output.txt"
Set-Content -Path "C:\file.txt" -Value "Content"

# Copy, move, delete
Copy-Item "source.txt" -Destination "dest.txt"
Move-Item "old.txt" -Destination "new.txt"
Remove-Item "file.txt"

System Information

# Get system information
Get-ComputerInfo
Get-ComputerInfo | Select-Object WindowsProductName, WindowsVersion

# Get processes
Get-Process
Get-Process | Where-Object {$_.CPU -gt 100}
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5

# Get services
Get-Service
Get-Service | Where-Object {$_.Status -eq "Running"}
Start-Service -Name "ServiceName"
Stop-Service -Name "ServiceName"

# Get network information
Get-NetIPAddress
Get-NetAdapter
Test-Connection google.com

Working with Variables

# Create variables
$name = "John"
$number = 42
$array = @(1, 2, 3, 4, 5)
$hash = @{Name="John"; Age=30}

# Variable types
[string]$text = "Hello"
[int]$number = 100
[bool]$flag = $true

# Special variables
$PSVersionTable        # PowerShell version
$HOME                  # User home directory
$PWD                   # Current directory
$env:Path              # Environment variable

The Pipeline

PowerShell's pipeline passes objects (not text) between commands:

# Basic pipeline
Get-Process | Where-Object {$_.CPU -gt 100} | Sort-Object CPU

# Process and format
Get-Service | Where-Object {$_.Status -eq "Running"} | Select-Object Name, Status | Format-Table

# Count items
Get-Process | Measure-Object | Select-Object Count

# Group items
Get-Process | Group-Object ProcessName

Filtering and Selection

Where-Object

Filter objects based on conditions:

# Filter processes
Get-Process | Where-Object {$_.CPU -gt 100}
Get-Process | Where-Object {$_.Name -like "*chrome*"}
Get-Process | Where-Object {$_.WorkingSet -gt 100MB}

# Multiple conditions
Get-Process | Where-Object {$_.CPU -gt 50 -and $_.Name -like "*win*"}

Select-Object

Choose specific properties:

# Select properties
Get-Process | Select-Object Name, Id, CPU
Get-Process | Select-Object -First 10
Get-Process | Select-Object -Last 5
Get-Process | Select-Object -Property Name, @{Name="Memory(MB)";Expression={$_.WorkingSet/1MB}}

Sort-Object

Sort objects:

# Sort by property
Get-Process | Sort-Object CPU
Get-Process | Sort-Object CPU -Descending
Get-Process | Sort-Object Name

PowerShell Scripting

Creating Scripts

PowerShell scripts use the .ps1 extension:

# Create a script
@"
Write-Host "Hello, PowerShell!"
Get-Date
"@ | Out-File "myscript.ps1"

# Run a script
.\myscript.ps1

# If execution policy blocks, allow scripts
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Basic Script Structure

# Script: hello.ps1
param(
    [string]$Name = "World"
)

Write-Host "Hello, $Name!"
Write-Host "Current date: $(Get-Date)"

Control Structures

If-Else:

$number = 10
if ($number -gt 5) {
    Write-Host "Number is greater than 5"
} elseif ($number -eq 5) {
    Write-Host "Number equals 5"
} else {
    Write-Host "Number is less than 5"
}

Switch:

$day = "Monday"
switch ($day) {
    "Monday"    { Write-Host "Start of work week" }
    "Friday"    { Write-Host "TGIF!" }
    "Saturday"  { Write-Host "Weekend!" }
    "Sunday"    { Write-Host "Weekend!" }
    default     { Write-Host "Midweek" }
}

Loops:

# For loop
for ($i = 1; $i -le 10; $i++) {
    Write-Host $i
}

# Foreach loop
$items = @("Apple", "Banana", "Cherry")
foreach ($item in $items) {
    Write-Host $item
}

# While loop
$counter = 0
while ($counter -lt 5) {
    Write-Host "Counter: $counter"
    $counter++
}

# Do-While loop
$counter = 0
do {
    Write-Host "Counter: $counter"
    $counter++
} while ($counter -lt 5)

Functions

Creating Functions

# Simple function
function Get-Greeting {
    param([string]$Name)
    return "Hello, $Name!"
}

Get-Greeting -Name "John"

# Function with default parameters
function Get-FileInfo {
    param(
        [string]$Path,
        [switch]$Detailed
    )
    
    $file = Get-Item $Path
    if ($Detailed) {
        return $file | Select-Object Name, Length, LastWriteTime, Attributes
    } else {
        return $file.Name
    }
}

Get-FileInfo -Path "C:\file.txt" -Detailed

Advanced Functions

function Get-SystemInfo {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$ComputerName,
        
        [Parameter(Mandatory=$false)]
        [switch]$IncludeServices
    )
    
    Write-Verbose "Getting system information for $ComputerName"
    
    $info = @{
        ComputerName = $ComputerName
        OS = (Get-CimInstance Win32_OperatingSystem).Caption
        Uptime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
    }
    
    if ($IncludeServices) {
        $info.Services = Get-Service
    }
    
    return [PSCustomObject]$info
}

Get-SystemInfo -ComputerName "localhost" -IncludeServices -Verbose

Error Handling

Try-Catch

try {
    Get-Content "nonexistent.txt" -ErrorAction Stop
} catch {
    Write-Host "Error: $($_.Exception.Message)"
} finally {
    Write-Host "Cleanup code here"
}

ErrorAction Parameter

# Continue on error (default)
Get-Content "file.txt" -ErrorAction Continue

# Silently continue
Get-Content "file.txt" -ErrorAction SilentlyContinue

# Stop on error
Get-Content "file.txt" -ErrorAction Stop

# Inquire
Get-Content "file.txt" -ErrorAction Inquire

Working with Modules

Finding Modules

# List installed modules
Get-Module -ListAvailable

# Find modules online
Find-Module -Name "*Azure*"

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

# Import module
Import-Module Az

Common Modules

  • Az: Azure PowerShell modules
  • ActiveDirectory: AD management
  • ExchangeOnlineManagement: Exchange Online
  • SharePointPnPPowerShellOnline: SharePoint
  • Pester: Testing framework

Practical Examples

Example 1: System Health Check

# Check system health
$health = @{
    ComputerName = $env:COMPUTERNAME
    Uptime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
    FreeSpace = (Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'").FreeSpace / 1GB
    RunningServices = (Get-Service | Where-Object {$_.Status -eq "Running"}).Count
    TotalProcesses = (Get-Process).Count
}

[PSCustomObject]$health | Format-List

Example 2: File Management

# Find large files
Get-ChildItem -Path C:\ -Recurse -File -ErrorAction SilentlyContinue | 
    Where-Object {$_.Length -gt 100MB} | 
    Sort-Object Length -Descending | 
    Select-Object FullName, @{Name="Size(MB)";Expression={[math]::Round($_.Length/1MB,2)}}

Example 3: Process Monitoring

# Monitor top CPU processes
while ($true) {
    Clear-Host
    Get-Process | 
        Sort-Object CPU -Descending | 
        Select-Object -First 10 Name, CPU, @{Name="Memory(MB)";Expression={[math]::Round($_.WorkingSet/1MB,2)}}
    Start-Sleep -Seconds 2
}

Example 4: Service Management

# Check and restart service
$serviceName = "Spooler"
$service = Get-Service -Name $serviceName

if ($service.Status -ne "Running") {
    Write-Host "Service is not running. Starting..."
    Start-Service -Name $serviceName
    Write-Host "Service started."
} else {
    Write-Host "Service is already running."
}

Best Practices

  1. Use Full Cmdlet Names: More readable than aliases
  2. Use Verb-Noun Naming: For custom functions
  3. Handle Errors: Use try-catch blocks
  4. Comment Your Code: Explain complex logic
  5. Use Parameters: Make scripts flexible
  6. Test Scripts: Test in safe environment first
  7. Use Modules: Reuse code through modules
  8. Follow Naming Conventions: Consistent naming
  9. Document Functions: Use comment-based help
  10. Version Control: Use Git for script management

PowerShell Resources

Learning Resources

  • Microsoft Learn: Official PowerShell documentation
  • PowerShell Gallery: Repository of scripts and modules
  • PowerShell.org: Community resources and forums
  • YouTube: Video tutorials and demos

Useful Commands

# Get help
Get-Help Get-Process
Get-Help Get-Process -Examples
Get-Help Get-Process -Full
Get-Help Get-Process -Online

# Update help
Update-Help

# Find commands
Get-Command *process*
Get-Command -Verb Get
Get-Command -Noun Process

Conclusion

PowerShell is an essential tool for Windows system administration and automation. This guide covered:

  • Basic PowerShell concepts and cmdlets
  • Working with objects and the pipeline
  • Scripting fundamentals
  • Functions and error handling
  • Practical examples

Continue learning by:

  • Practicing with daily tasks
  • Exploring Get-Help for commands
  • Reading PowerShell scripts
  • Building your own automation tools
  • Joining PowerShell communities

PowerShell mastery takes time, but the investment pays off in increased productivity and automation capabilities.