PowerShell Scripting Basics

My First Production Script

I'll never forget my first "real" PowerShell script. Our team was manually provisioning user accounts - a 30-minute process per user involving Active Directory, Exchange, file share permissions, and group memberships. With 50 new hires starting next month, we were looking at 25 hours of repetitive work.

I spent an evening writing a PowerShell script:

.\New-UserAccount.ps1 -FirstName "John" -LastName "Doe" -Department "IT"

What took 30 minutes manually now took 90 seconds. More importantly, it was consistent - no missed steps, no typos in group names, no forgotten permissions. That script ran for 3 years, creating over 500 user accounts without a single error.

That's when I learned the difference between typing commands interactively and writing reusable scripts.

Creating PowerShell Scripts

Script Files (.ps1)

PowerShell scripts are text files with .ps1 extension:

# Save as: Get-SystemInfo.ps1
$computerName = $env:COMPUTERNAME
$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem

Write-Host "Computer: $computerName"
Write-Host "OS: $($osInfo.Caption)"
Write-Host "Version: $($osInfo.Version)"

Running Scripts

Dot Sourcing

Dot sourcing loads script content into current session:

Execution Policies

Windows PowerShell has execution policies to prevent accidental script execution.

Checking Execution Policy

Policy Levels

  • Restricted: No scripts allowed (default on Windows client)

  • AllSigned: Only signed scripts

  • RemoteSigned: Local scripts ok, downloaded must be signed (recommended)

  • Unrestricted: All scripts allowed (requires confirmation)

  • Bypass: Nothing is blocked, no warnings

Setting Execution Policy

Note: Execution policies are not security features. They prevent accidental execution but can be easily bypassed by determined users.

Script Parameters

Parameters make scripts reusable.

Basic Parameters

Usage:

Typed Parameters

Multiple Parameters

Usage:

Mandatory Parameters

If mandatory parameter is missing, PowerShell prompts for it.

Parameter Validation

ValidateSet

Restrict to specific values:

ValidateRange

Numeric ranges:

ValidatePattern

Regex validation:

ValidateLength

String length:

ValidateScript

Custom validation:

Real-World Example

Comment-Based Help

Make your scripts self-documenting:

View help:

Script Structure Best Practices

Organized Script Template

Using #Requires

Ensure prerequisites:

Real-World Script Example

Common Pitfalls

1. Not Using Parameters

Problem: Hard-coded values make scripts inflexible Solution: Use parameters for anything that might change

2. Missing Parameter Validation

Problem: Scripts fail with cryptic errors Solution: Validate parameters at the start

3. No Error Handling

Problem: Scripts fail silently or with unclear messages Solution: We'll cover error handling in the next article

4. Forgetting Execution Policy

Problem: Scripts won't run on new systems Solution: Document execution policy requirements

Key Takeaways

  • .ps1 files contain PowerShell scripts

  • Execution policies prevent accidental execution

  • Parameters make scripts reusable and flexible

  • Validation attributes ensure correct input

  • Comment-based help documents your scripts

  • Organized structure improves maintainability

  • #Requires ensures prerequisites are met

What You've Learned

✅ Creating and running PowerShell scripts ✅ Understanding and setting execution policies ✅ Using parameters with types and defaults ✅ Parameter validation techniques ✅ Comment-based help for documentation ✅ Script structure best practices ✅ Real-world script examples

Next Steps

Now that you can create scripts with parameters, you need control flow to make decisions. In Control Flow and Loops, you'll learn:

  • If/ElseIf/Else statements

  • Switch statements

  • Loops (foreach, for, while)

  • break and continue

  • Processing collections efficiently

Control flow is what makes scripts intelligent - let's add decision-making to your scripts.


Ready to add logic to your scripts? Continue to Control Flow and Loops

Last updated