Exercise: What a File Knows

Answer a friend's questions about their trip photos by asking the objects themselves, with Get-Member, properties, methods and Select-Object, save a plain list and a table of sizes, and let a Pester check tell you what's done.

Apprentice · PowerShell · Updated

A friend came back from a weekend at the coast and handed you the photos, with a few questions. What are these files, really? How big is the one of the harbor? Which one comes last? They'd also like two lists to keep: the photo names, and a table of names and sizes. Everything you need is in this chapter. You'll find each answer by asking the objects themselves, and a check tells you which are done.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-powershell-what-a-file-knows, also linked from the Exercise box beside the lesson. Click Code, then Download ZIP, and unzip it into your terminal-practice folder (with Expand-Archive, if you like). Or clone it with Git.

The folder holds:

  • photos: the five photos from the trip. Ask them questions; don't rename or change them.
  • answers.txt: one line for each question, for your answers.
  • Check.Tests.ps1: the check. You don't need to change it.

What to do

Open PowerShell 7 and go into the exercise folder with Set-Location. The questions are in the folder's README.md:

  1. Answer six questions by asking objects: what kind of object a photo is, how big one photo is, two days of the week worked out from a date, which property says when a program started, and which file Get-ChildItem lists last.
  2. Save the photo names in photo-names.txt, one per line, with no heading.
  3. Save a table of the photos' names and sizes in photo-sizes.txt, exactly as it looks on the screen.

Save both files in the exercise folder itself, not in photos. Write each answer after its number in answers.txt, such as 2) 1234, and save the file. Open it with edit answers.txt, or in Notepad.

# Write each answer after its number, on the same line. See README.md for the questions.
1)
2)
3)
4)
5)
6)

Check your work

The check uses Pester, PowerShell's test tool. Install it once, if you haven't already:

Install-PSResource Pester

Then, from the exercise folder:

Invoke-Pester

At first all eight tests fail. That's expected: each step you finish turns its test green. A failing test says what it was looking for, for example Expected a value, because question 2 has no answer yet. Read the Length property of photos\harbor-at-dawn.jpg with a dot, but got $null or empty.

If PowerShell says the check can't be loaded because running scripts is disabled or the file is not digitally signed, Windows has marked the downloaded files as coming from the internet. From the exercise folder, run Get-ChildItem -Recurse | Unblock-File once, then Invoke-Pester again. Why a Script Won't Run: The Execution Policy, in Module 5, explains what that does.

Hints

  • Read the card first. Get-Member -MemberType Property lists every property's name. A property that prints nothing is usually a misspelled one.
  • A date is an object too. Get-Date 2026-12-25 gets that day as a date object, with the same properties and methods as Get-Date on its own.
  • Methods need their parentheses, with what they work with inside them. Then another dot reads a property of what comes back.
  • Plain list or table? One of the two files wants -ExpandProperty; the other wants a Format- command, and only > after it. Tables and Lists: Format-Table and Format-List shows why Set-Content after Format-Table goes wrong.

When you're done

Every test is green, and Pester's last line says:

Tests Passed: 8, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0

Now look at question 6 once more. If you wanted the biggest photo instead of the last one by name, what would have to happen before Select-Object -First 1?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

  1. On GitHub, click +, then New repository. Name it (for example exercise-powershell-what-a-file-knows), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In a terminal in your exercise folder (PowerShell works), send your work to your new repo. If you downloaded the ZIP:

    git init -b main
    git add .
    git commit -m "My solution"
    git remote add origin https://github.com/your-name/exercise-powershell-what-a-file-knows.git
    git push -u origin main
    

    If you cloned the starter instead, point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-powershell-what-a-file-knows.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-powershell-what-a-file-knows, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

You asked files, dates and a running program what they know, and saved the answers two ways: as plain values and as a table. That's the habit the rest of this module builds on: find out what an object knows, then pick, sort and filter by it.

Key terms

Related