PowerShell Fundamentals

Variables Collections and Conditions

Use PowerShell variables, arrays, hashtables, comparisons, and collection pipelines with visible types and results.

Beginner14 min read
PowerShell Fundamentals lessonAutomation foundationsLearn

Use PowerShell variables, arrays, hashtables, comparisons, and collection pipelines with visible types and results.

What you will be able to do

  • Explain the five core programming decisions involved in variables collections and conditions.
  • Choose a small code or command method that matches a stated input and output need.
  • Interpret execution output and errors before changing broader system state.
  • Apply an inspect, implement, test, verify, and recover workflow to a realistic automation task.

01

Build the Execution Model

Use PowerShell variables, arrays, hashtables, comparisons, and collection pipelines with visible types and results.

Reliable automation separates input, representation, decision, action, output, and failure behavior. Before writing a longer script, state the exact starting data, the intended transformation, the observable result, and which state the program is permitted to change.

Use a disposable project and small deterministic samples. Run read-only inspection first, keep the interpreter or shell and working directory visible, and preserve the exact input with its output. A reproducible example is more useful than a large script whose state is unknown.

02

PowerShell variables

A PowerShell variable stores an object under a name in a scope. It is one distinct part of variables collections and conditions, so keep its input and output visible instead of hiding them inside a larger unexplained script.

Choose a descriptive name and inspect the object's type and value. Start with this small method: Display the variable and call `GetType()`. Record the code, interpreter or shell version, working directory, sample input, and visible result together.

The safety boundary is clear: A variable can later hold a different type. The expected evidence is The powershell variables result matches the documented input and expected state. If the result differs, keep the failure and reduce the example before changing unrelated code or system state.

03

Array collections

An array is an ordered collection whose pipeline enumeration normally emits its elements. It is one distinct part of variables collections and conditions, so keep its input and output visible instead of hiding them inside a larger unexplained script.

Test count, indexing, and enumeration with a small sample. Start with this small method: Wrap sample output with `@(...)`. Record the code, interpreter or shell version, working directory, sample input, and visible result together.

The safety boundary is clear: A single result and an array can behave differently. The expected evidence is The array collections result matches the documented input and expected state. If the result differs, keep the failure and reduce the example before changing unrelated code or system state.

04

Hashtable mapping

A hashtable maps unique keys to values for lookup and structured configuration. It is one distinct part of variables collections and conditions, so keep its input and output visible instead of hiding them inside a larger unexplained script.

Use stable key names and check membership before access. Start with this small method: Create and query a small hashtable. Record the code, interpreter or shell version, working directory, sample input, and visible result together.

The safety boundary is clear: Key spelling and case behavior can affect interoperability. The expected evidence is The hashtable mapping result matches the documented input and expected state. If the result differs, keep the failure and reduce the example before changing unrelated code or system state.

05

Comparison operators

PowerShell comparison operators produce booleans or matching collection elements depending on input shape. It is one distinct part of variables collections and conditions, so keep its input and output visible instead of hiding them inside a larger unexplained script.

Inspect whether the left operand is scalar or collection. Start with this small method: Compare scalar and array inputs. Record the code, interpreter or shell version, working directory, sample input, and visible result together.

The safety boundary is clear: Collection comparison may return items instead of one boolean. The expected evidence is The comparison operators result matches the documented input and expected state. If the result differs, keep the failure and reduce the example before changing unrelated code or system state.

06

Collection pipeline

ForEach-Object transforms items while Where-Object filters them by a predicate. It is one distinct part of variables collections and conditions, so keep its input and output visible instead of hiding them inside a larger unexplained script.

Keep each pipeline stage focused and inspect intermediate objects. Start with this small method: Run a three-item filter and transform. Record the code, interpreter or shell version, working directory, sample input, and visible result together.

The safety boundary is clear: Nested script blocks can hide state changes. The expected evidence is The collection pipeline result matches the documented input and expected state. If the result differs, keep the failure and reduce the example before changing unrelated code or system state.

07

Implement One Small Behavior

Translate the requirement into one input-output example before implementation. Name the target and side effects explicitly, validate input at the program boundary, and keep secrets and environment-specific configuration outside source code and ordinary logs.

Add the smallest code that makes the example work. Inspect intermediate values as structured data, not only formatted display text. When an operation can modify files, accounts, services, remote systems, or API resources, build a preview or disposable test path first.

Handle the expected failure that belongs to the operation. Preserve its type, message, target, and timing, then return a meaningful result or nonzero exit. Do not catch errors the program cannot explain merely to make the run appear successful.

08

Test and Handoff the Automation

Test a normal input, one boundary input, and one safe failure. Compare actual output with the written expectation and verify any changed state through an independent read-only query. A zero exit code or successful request is only part of the evidence.

Make the run reproducible. Record runtime version, dependencies, parameters, configuration source, sample data, output shape, and cleanup or reversal procedure. Remove temporary credentials, test files, sessions, and permissions after verification.

A useful handoff explains what the program accepts, what it returns or changes, how it reports failure, and where execution must stop for review. Escalate when authorization, data ownership, target scope, vendor contract, or safe recovery remains uncertain.

09

Recap Before Practice and Prove

Start with powershell variables. Choose a descriptive name and inspect the object's type and value. Verify that the powershell variables result matches the documented input and expected state.

Keep array collections explicit. Test count, indexing, and enumeration with a small sample. Respect this boundary: a single result and an array can behave differently..

Use hashtable mapping as a separate decision. Use stable key names and check membership before access. Preserve the exact input and output.

Before expanding the script, review comparison operators. Inspect whether the left operand is scalar or collection. Stop when collection comparison may return items instead of one boolean..

Finish with collection pipeline. Keep each pipeline stage focused and inspect intermediate objects. Record the final result, failure behavior, cleanup, and reproducible next step.

NEXT STEP

Turn reading into recall

Practice the concepts without a timer, with coaching and retry available after every answer.

Open guided practice