Skip to main content
Free managed IT onboarding through September 30For qualifying new clients. No long-term contract.View offer
KaselTech
Back to Blog
Automation

PowerShell Automation: Getting Started Guide

By KaselTech Team · September 15, 2024 · 6 min read

Table of Contents

If you're still manually performing repetitive IT tasks, you're working too hard. PowerShell can automate almost anything in the Windows and Microsoft ecosystem. Here's how to get started.

  • -Local Windows systems
  • -Active Directory
  • -Microsoft 365
  • -Azure
  • -Exchange Online
  • -Network devices
  • -And much more

One script can save hours of manual work every week.

Opening PowerShell

  • -Windows 10/11: Right-click Start, select "Windows Terminal (Admin)"
  • -Windows Server: Search for "PowerShell" and run as Administrator

Your First Commands

Try these to get comfortable:

# Get computer information

# List running services Get-Service | Where-Object Status -eq "Running"

# Check disk space Get-PSDrive -PSProvider FileSystem ```

Understanding the Verb-Noun Pattern

PowerShell commands (cmdlets) follow a consistent pattern:

  • -Get- retrieves information
  • -Set- modifies settings
  • -New- creates something
  • -Remove- deletes something
  • -Start-/Stop- controls processes and services

Example 1: User Onboarding Script

Creating new users manually is tedious and error-prone:

# Create new AD user with standard settings

New-ADUser -Name "John Smith" -GivenName "John" -Surname "Smith" -SamAccountName "jsmith" -UserPrincipalName "jsmith@company.com" -Path "OU=Users,DC=company,DC=com" -AccountPassword $Password -ChangePasswordAtLogon $true -Enabled $true

# Add to standard groups Add-ADGroupMember -Identity "All Employees" -Members "jsmith" ```

Example 2: Stale Computer Cleanup

Find computers that haven't logged in recently:

# Find computers inactive for 90+ days

Get-ADComputer -Filter {LastLogonDate -lt $90DaysAgo} -Properties LastLogonDate | Select-Object Name, LastLogonDate | Export-Csv "StaleComputers.csv" -NoTypeInformation ```

Example 3: Disk Space Monitoring

Check disk space across multiple servers:

foreach ($Server in $Servers) { Get-WmiObject Win32_LogicalDisk -ComputerName $Server | Where-Object DriveType -eq 3 | Select-Object @{N="Server";E={$Server}}, DeviceID, @{N="SizeGB";E={[math]::Round($_.Size/1GB,2)}}, @{N="FreeGB";E={[math]::Round($_.FreeSpace/1GB,2)}}, @{N="PercentFree";E={[math]::Round($_.FreeSpace/$_.Size*100,2)}} } ```

Example 4: Microsoft 365 License Report

See who has which licenses:

# Connect to Microsoft Graph

# Get all licensed users Get-MgUser -All -Property DisplayName,UserPrincipalName,AssignedLicenses | Where-Object AssignedLicenses | Select-Object DisplayName, UserPrincipalName | Export-Csv "LicensedUsers.csv" -NoTypeInformation ```

Create a script file (.ps1) and schedule it with Task Scheduler:

  1. Open Task Scheduler
  2. Create Basic Task
  3. Set trigger (daily, weekly, etc.)
  4. Action: Start a program
  5. Program: powershell.exe
  6. Arguments: -ExecutionPolicy Bypass -File "C:\Scripts\YourScript.ps1"

1. Test Before Automating

Always test with -WhatIf when available:

Remove-ADUser -Identity "jsmith" -WhatIf

2. Add Error Handling

try {
    # Your code here
    Get-ADUser -Identity "jsmith"
}
catch {
    Write-Error "Failed: $_"
    # Log or notify
}

3. Log Everything

Start-Transcript -Path "C:\Logs\Script-$(Get-Date -Format 'yyyyMMdd').log"
# Your script here
Stop-Transcript

4. Use Comments

# This script checks for inactive users
# Author: IT Team
# Last Updated: 2024-09-15
  • -Microsoft Learn: Free PowerShell training modules
  • -PowerShell Gallery: Pre-built modules and scripts
  • -Get-Help: Built-in documentation (Get-Help Get-Service -Full)

Our team builds custom PowerShell solutions for businesses of all sizes. From simple scripts to complex automation workflows, we can help you save time and reduce errors. Contact us for a consultation.

Keep Reading

Automation

Stop Renting AI: Why Custom Business Applications Beat Another SaaS Subscription

Read article
Managed IT

How to Switch IT Providers Without Losing Passwords, Licenses, or Control

Read article
Compliance

CMMC Phase 2 Is Suspended. Your Obligations Are Not.

Read article

Need Help With This?

Our team specializes in helping businesses nationwide with automation solutions.

Get in Touch