PowerShell Basics Level: Beginner Chapter 4: Everyday Commands That Save Hours

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.

Lesson 3 of 4 in Chapter 4: Everyday Commands That Save Hours. Previous step in this path: Verbose, WhatIf, and Confirm. 3 learning objectives, 7 examples, and a knowledge check.

Start: what you will learn

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

Learning objectives

  • Format results as a table or a list
  • Ask for input with Read-Host
  • Explain why formatting should be the last step

Concept in simple words

What it is: formatting commands control how results look on screen, and input commands ask you a question. Format-Table shows results as columns, like a small table. Format-List shows one item at a time, property by property, down the page. Read-Host asks you to type an answer, and Write-Output sends a value onward or shows it. Formatting is about looking, input is about asking, and output is about passing a value on.

Why it works

Why you need it: a good answer is not only correct, it is readable. Ten properties across the screen in a cramped row are hard to compare, and one important property hidden in a long row is easy to miss. Choosing a table for comparison, or a list for detail, is what turns raw data into an answer a person can actually use. Asking a clear question with Read-Host is the same skill in reverse, because a vague prompt gets a vague or wrong answer.

Why it works this way: PowerShell separates the data from the display. The objects stay the same underneath, and the formatting command only decides how they are shown at the very end. That separation is powerful, but it has a catch. Once you format, the result is no longer your original objects, so later commands cannot filter or sort it in the same way. Think of formatting as printing a photo of your data. The photo looks nice, but you cannot edit the original spreadsheet from the photo.

Think of it like this

Think of it like this: imagine you have a spreadsheet of facts. Format-Table is printing a neat summary table from it. Format-List is opening one record at a time and reading every field aloud. Read-Host is simply stopping to ask you a question before continuing, like a form asking for your name before it can look something up. Write-Output is handing a note to the next person in line, so they can use it, while simply showing a message on screen is like pinning that note to the wall where nobody can pick it up.

How it works, step by step

How it works, step by step: first, shape your results with Get, Where-Object, Sort-Object, and Select-Object, exactly as you learned in the last lessons. Next, choose a table for many items with few properties, or a list for one item with many properties. Then add Format-Table -AutoSize if you want the columns to fit the content, or Format-List with the property names you want to see. Finally, read the display and check that it answers your question, because formatting is for looking, not for further processing.

Format-Table in depth: Format-Table takes the properties you name and puts each one in a column. -AutoSize adjusts the column widths to fit the data, instead of using fixed widths that can cut values off. -Wrap lets a long value continue on the next line inside its column, instead of being cut off with dots. -HideTableHeaders hides the header row, which is occasionally useful for a compact display, but it also removes the labels that explain each column, so use it only when the columns are obvious. -RepeatHeader repeats the header after each screen of output, which helps when a long table scrolls past one screen.

Format-Table -GroupBy in depth: -GroupBy splits the table into groups that share one property value, for example one group per Status value. Because grouping expects sorted input, sort by the grouping property first, with Sort-Object Status, and then use Format-Table -GroupBy Status. If you skip the sort, the same group can appear in several separate blocks, which looks messy and is hard to read. Grouping is a display choice, not a filter, so it does not remove any rows, it only organises how they appear.

Format-List in depth: Format-List shows each object as a block of labelled lines, one property per line, in the form Name : value. That is ideal when one item has many properties, or when a property value is long, because a table row would squeeze it. You can name the properties you want, as in Format-List Name, Status, DisplayName, or use Format-List * to show all properties of one item. The star form is a great way to discover what properties an object actually has, but it is a poor way to show a hundred items, because the screen fills with repeated blocks.

Format-Wide in depth: Format-Wide shows a single property in several columns across the screen, like a compact list of names. Use it when you only need one fact per item, for example a list of service names, and you want to fit many of them on screen at once. -Column controls how many columns are used, as in Format-Wide -Column 3. Format-Wide is for a quick overview, not for comparing several properties side by side, because it only shows one property.

What formatting does to the pipeline: after a Format command, the pipeline no longer carries your original objects, it carries formatting instructions. That is why Where-Object, Sort-Object, Select-Object, and export commands stop working sensibly after a Format command. They receive formatting objects, not services or processes, so a filter on Status finds nothing useful. The rule is simple and strict: shape, filter, and sort first, export or process next if you need to, and format only at the very end, for display.

Truncation, and how to spot it: when a value is too long for its column, PowerShell may show the start of the value followed by dots, which means the rest is hidden, not missing. If a value looks cut off, try -AutoSize, or -Wrap, or switch to Format-List for that item, where long values have room. Never copy a truncated value into a report or a next command, because the dots are not part of the real value, and the hidden part may be exactly the part you needed.

Read-Host in depth: Read-Host shows a prompt, waits for you to type an answer, and returns that answer when you press Enter. The prompt text should say exactly what you want, and in what form, for example "Enter a service name" is clearer than "Enter a value". You normally store the answer in a variable, as in $name = Read-Host "Enter a service name", and then use that variable in the next command. If you do not store it, the answer is shown and then lost, which is rarely what you want.

What Read-Host returns: Read-Host always returns text, even if you typed digits. That matters when you later compare or calculate with the answer. A number typed at a prompt is still text until you convert it, for example by writing [int]$count = Read-Host "Enter a number", which converts the text to a whole number. If the typed answer is not a valid number, that conversion fails, which is actually useful, because it tells you the input was wrong, instead of silently treating text as a number.

Read-Host and empty answers: if you press Enter without typing anything, Read-Host returns an empty string, not an error. Your next command may then fail, or worse, act on an empty value. A simple guard is to test the answer with [string]::IsNullOrWhiteSpace($name), which is true when the answer is empty or only spaces. Checking for empty input is a small habit that prevents a whole class of confusing errors, and it shows the person using your script that you thought about what could go wrong.

Read-Host for sensitive input: for a password or another secret, never use a plain Read-Host prompt, because the typed characters appear on screen in plain text. Use Read-Host -AsSecureString, which masks the typed characters with asterisks and returns a secure string object, not plain text. Do not print that secure string, and do not convert it back to plain text just to check it, because that defeats the purpose. A secure string is a container for a secret, not a value to display.

Write-Output in depth: Write-Output sends a value to the success output stream, which is the main pipeline stream. That means the value can be shown on screen, stored in a variable, or piped to the next command. For example, $message = Write-Output "Ready" stores the text Ready in $message, because Write-Output sends it onward instead of only printing it. This is the key difference from a command that only writes to the screen: a screen only message cannot be captured or piped, while a Write-Output value can be used by the next step.

When to use it

When to use Read-Host, and when not to: use Read-Host for a quick interactive question, but not in a script that must run on its own, because nobody will be there to type an answer. For a script that runs on a schedule or on another computer, use a parameter instead, so the value can be supplied when the script starts, without stopping for input. A script that pauses for input in the middle of the night will simply wait, and that wait can look like a hang, when the real problem is a missing person to answer.

Let us walk through an example: in Get-Service -Name Spooler | Format-List Name, Status, DisplayName, the first part gets one service object. Format-List then shows that one service property by property, instead of squeezing it into a row. DisplayName is the friendly name shown to people, which can differ from the short service name. Look for each label on its own line, and check that the values match the service you asked for. If you see a label with no value, check the property spelling, and if you see far more labels than you asked for, check that you did not use * by accident.

When to use it, and when not to: use Format-Table when you want to compare many rows side by side, and Format-List when one item has so many properties that a row would be cramped or cut off. Use Format-Wide for a compact list of one property. Use Read-Host for a quick interactive question, but not in a script that must run on its own, because nobody will be there to type an answer. Never format in the middle of a pipeline and then try to filter, sort, or export the result.

Worked examples

Read each example in order. Each one explains its parts, then shows what to look for in the output.

Syntax

Syntax
Get-Command | Format-Table | Format-List

Example 1

Shapes first, formats last. AutoSize fits the columns to the content. Select-Object keeps only Name and CPU, -First 3 keeps three rows, and Format-Table is the final step, for display only. Look for fitted columns with no cut off values.

Example 1
Get-Process | Select-Object Name, CPU -First 3 | Format-Table -AutoSize

Illustrative output: Name CPU ---- --- pwsh 12.5

Example 2

Asks for a name, stores it, then uses it. The second command does the real work. The first line shows the prompt, waits for typing, and stores the answer in $name. The second line uses $name as the service name. If you type a name that does not exist, the second command reports an error, which is a sign to check the spelling.

Example 2
$name = Read-Host "Enter a service name"
Get-Service -Name $name

Illustrative output: Enter a service name: Spooler

Example 3

Get-Service -Name Spooler gets one service object, the Spooler service. The pipe sends that object to Format-List. Format-List shows the named properties one per line, so Name, Status, and DisplayName are each easy to read. DisplayName is the friendly name for the service.

Example 3
Get-Service -Name Spooler | Format-List Name, Status, DisplayName

Illustrative output: Name : Spooler Status : Running DisplayName : Print Spooler

Example 4

Sort-Object Status comes first, because grouping expects sorted input. Format-Table then shows Name and Status in columns, split into one group per Status value. -AutoSize fits the columns to the content. Look for one heading per Status value, with only services of that status under it. If the same status heading repeats, the sort step was probably missing.

Example 4
Get-Service | Sort-Object Status | Format-Table Name, Status -GroupBy Status -AutoSize

Illustrative output: Name Status ---- ------ Spooler Running

Example 5

Select-Object -ExpandProperty Name returns plain name values, which is what Format-Wide needs, because Format-Wide shows one property. -First 6 keeps six names, and -Column 3 lays them out in three columns across the screen. Look for a compact grid of names with no other columns. This form is for a quick overview, not for comparing Status values.

Example 5
Get-Service | Select-Object -ExpandProperty Name -First 6 | Format-Wide -Column 3

Illustrative output: Spooler

Example 6

Read-Host asks for a password, -Prompt sets the question text, and -AsSecureString masks the typed characters with asterisks and stores the result as a secure string in $password. Do not print $password to check it, because that would expose the secret the masking was meant to protect. Look for asterisks while typing, not for the typed characters themselves.

Example 6
$password = Read-Host -Prompt "Enter a password" -AsSecureString

Illustrative output: Enter a password: *****

Example 7

The first line stores the typed answer in $name. The second line tests whether that answer is empty or only spaces, using [string]::IsNullOrWhiteSpace. If the test is true, Write-Output sends the message "No name entered" onward, instead of running a lookup with an empty name. Look for the message when you press Enter without typing, and look for no message when you type a real name.

Example 7
$name = Read-Host "Enter a service name"
if ([string]::IsNullOrWhiteSpace($name)) { Write-Output "No name entered" }

Illustrative output: No name entered

Nothing on this page executes PowerShell code. The outputs above are illustrative only.

Common mistakes and troubleshooting

Read these before you try the examples, so you can spot the same problems in your own work.

  • Putting Format-Table in the middle of a pipeline, then trying to filter or export the result. Format last.
  • Using Read-Host in a script that must run on a schedule, where nobody is there to answer. Use a parameter instead.
  • Using Format-List for a long list of items, so the screen fills with repeated blocks and you cannot compare rows at a glance. A list suits one item, while a table suits many rows you want to compare.
  • Copying a truncated value, with dots at the end, into a report or a next command. The dots mean hidden text, not part of the value, so use -Wrap, -AutoSize, or Format-List to see the full value first.
  • Using a plain Read-Host prompt for a password, so the secret appears on screen in plain text. Use -AsSecureString for secrets, and do not print the result.
  • Treating a Read-Host answer as a number without converting it. Read-Host returns text, so convert it first, for example with [int], when you need a number, and expect an error if the typed answer is not numeric.
  • Forgetting to store a Read-Host answer in a variable. Without a variable, the answer is shown and then lost, and the next command has nothing to use.

Knowledge check

Knowledge check

Where should Format-Table go in a pipeline?

Pick one answer. Feedback appears right away, and you can try again.

Remember this and next step

Remember this: shape first, format last, and only for display. If a later step stops working, check whether a Format command got in the way, and move it to the end. If a value looks cut off, check for truncation before you assume data is missing. If a prompt answer behaves strangely, check whether it is text when you expected a number, or empty when you expected a value.

Next step: Multiple lines in a console