PowerShell Basics

Chapter 4 of 18

Chapter 4: Everyday Commands That Save Hours

Welcome to the chapter where PowerShell starts to feel really useful. You already know how to get results, and now you will learn how to shape them, check them safely, and show them clearly. You will pick just the facts you need, keep only the rows that matter, and put them in a sensible order. You will also learn how to preview a change before you make it, so you can practise without fear. By the end of this chapter, you will be able to take a messy pile of results and turn it into a short, clear answer you trust.

4 lessons 3 learning objectives 11 related commands

Topics at a glance: Select-Object, Where-Object, and Sort-Object, Verbose, WhatIf, and Confirm, Formatting, input, and output display, Multiple lines in a console.

Where this chapter fits

This chapter comes after Chapter 3: Think in Objects, Not Text in the journey, so ideas from that chapter will feel familiar here.

After this chapter comes Chapter 5: Turn Commands Into Scripts You Can Reuse.

Learning objectives

By the end of this chapter, you will be able to:

  • Shape output with Select, Where, and Sort
  • Preview and confirm changes safely with WhatIf and Confirm
  • Format output and ask for input clearly

Lessons in this chapter

Work through the lessons in order. Each lesson page has explanations, examples, common mistakes, and a knowledge check.

  1. Lesson 1: Select-Object, Where-Object, and Sort-Object

    These three cmdlets shape a result: Select picks properties, Where keeps matching objects, and Sort orders them. Used together, they answer most "show me" questions.

    3 things to learn in this lesson

  2. Lesson 2: Verbose, WhatIf, and Confirm

    Three safety switches change how a command behaves: Verbose explains what it is doing, WhatIf previews without changing anything, and Confirm asks before each change.

    3 things to learn in this lesson

  3. Lesson 3: Formatting, input, and output display

    Formatting cmdlets control how results look on screen. Read-Host asks the user for input, and Write-Output sends a value down the pipeline.

    3 things to learn in this lesson

  4. Lesson 4: Multiple lines in a console

    Long commands are easier to read on several lines. Shift plus Enter continues a line, and braces group a block of code together.

    3 things to learn in this lesson

Related command references

These command pages are linked to lessons in this chapter. Each command page has its syntax, parameters, and examples.

  • Get-Service

    Lists services with their status, name, and display name.

    From the lesson: Select-Object, Where-Object, and Sort-Object

  • Set-Service

    Changes a service, such as its startup type or status settings.

    From the lesson: Select-Object, Where-Object, and Sort-Object

  • Restart-Service

    Stops and then starts a service again.

    From the lesson: Select-Object, Where-Object, and Sort-Object

  • Stop-Service

    Stops a running service.

    From the lesson: Verbose, WhatIf, and Confirm

  • Select-Object

    Picks properties, or the first or last objects, from input.

    From the lesson: Select-Object, Where-Object, and Sort-Object

  • Where-Object

    Keeps only objects that pass a test.

    From the lesson: Select-Object, Where-Object, and Sort-Object

  • Sort-Object

    Orders objects by a property, ascending or descending.

    From the lesson: Select-Object, Where-Object, and Sort-Object

  • Format-Table

    Formats objects as a table, for display only. Use it last.

    From the lesson: Formatting, input, and output display

  • Format-List

    Formats objects as a list, one object at a time, for display only.

    From the lesson: Formatting, input, and output display

  • Read-Host

    Asks the user for input and returns it as text.

    From the lesson: Formatting, input, and output display

  • Write-Output

    Sends a value to the pipeline, which usually displays it.

    From the lesson: Formatting, input, and output display

Tip: read the lesson first, then use the command page as a quick reference.