menno.cloud
  • Home
  • About
  • Skills
  • Experiences
  • More
    Certifications
  • Posts
  • Dark Theme
    Light Theme Dark Theme System Theme
  • Posts
  • PowerShell Scripting 101
    • Part 1 - Command to Script
    • Part 2 - Variables, scoping, flow
    • Part 3 - Functions
    • Part 4 - Error Handling and Logs
    • Part 5 - A Real Script
Hero Image
Part 1 - From Command to Script

Most sysadmins and cloud engineers start with PowerShell by running one-liners in the console: querying services, restarting computers, or pulling Azure resource data. It’s fast, effective, and—until it’s not. The moment you want reusability, error handling, input validation, or logging, that one-liner becomes a liability. That’s where scripting comes in. This post walks you through the mental shift from running commands to writing scripts—without assuming any prior scripting experience. Scripts vs. Commands A command is what you type into the console. A script is a saved sequence of commands (typically in a .ps1 file) that you can run, reuse, and version. It lives outside your head—and outside your clipboard history.

Wednesday, August 6, 2025 Read
Hero Image
Part 2 - Variables, scope and flow control

Now that you’ve written your first .ps1 script, it’s time to move past static commands. This post covers variables, scope, and flow control. The building blocks of decision-making in your scripts. Once you grasp these, you can write logic that adapts to different inputs, environments, or outcomes. Declaring and Using Variables Variables in PowerShell always begin with a dollar sign: $ServiceName = 'wuauserv' They’re typeless by default but support strong typing when needed:

Wednesday, August 6, 2025 Read
Hero Image
Part 3 - Functions

By now, you’ve written linear scripts with logic and variables. But eventually, you’ll want to reuse code, reduce repetition, and make your scripts easier to test and maintain. That’s where functions come in. This post shows you how to write clean, reusable functions using PowerShell best practices, with parameter validation, output handling, and real-world context. Why Functions Matter Functions let you encapsulate logic into a named block that: Can be reused multiple times Accepts input (parameters) Returns output (via Write-Output) Keeps your script clean and testable Think of functions as power tools for scripting: they do one job, and do it well.

Wednesday, August 6, 2025 Read
Hero Image
Part 4 - Error Handling and Logs

Once your scripts go beyond test labs and start interacting with live environments—local systems, production servers, Azure resources. They will encounter errors. That’s not failure. That’s reality. What matters is how you handle it. This post covers clean, predictable error handling and structured logging. We’ll move beyond -ErrorAction SilentlyContinue and into something maintainable. The Wrong Way: Swallowing Errors This pattern is everywhere in beginner scripts: Get-Service -Name 'InvalidName' -ErrorAction SilentlyContinue It suppresses the error and gives you no feedback. That’s not handling an error—that’s ignoring one.

Wednesday, August 6, 2025 Read
Hero Image
Part 5 - A Real Script

So far, you’ve learned how to think in scripts, declare variables, handle logic, structure functions, and manage errors and logging. In this final post, we’ll build a complete script that combines everything. The script will: Accept input from a JSON file Validate and process that input Create Azure or local resources based on parameters Handle errors and log results Follow PowerShell best practices throughout Scenario: Provision Virtual Machines from JSON Input Let’s say your team provides you with a JSON file like this:

Wednesday, August 6, 2025 Read
Contact me:
  • menno@korthoff-it.nl
  • mystacx
  • Menno Korthoff

Toha Theme Logo Toha
© 2025 Copyright.
Powered by Hugo Logo