Your First Script

A script is an application: write one, run it, and start it from anywhere, even a double-click.

Updated

Scripts

  1. A Script Is an Application (Novice): Every application takes something in, does some work and gives something back, and a PowerShell script does the same from a .ps1 text file you can read, which makes it a quick-run application of your own, best at jobs made of files, data, settings and other programs.
  2. Writing Scripts in VS Code (Apprentice): VS Code with Microsoft's free PowerShell extension colors a script, suggests commands as you type, underlines problems such as a missing quote or an alias before you run anything, and runs the whole script with F5 in a terminal inside the editor.
  3. Your First Script (Apprentice): A script is a .ps1 file of commands that run from top to bottom, with comments starting with # that say what it does, and you run it from its own folder with .\ and its name, because PowerShell never looks in the current folder for a command on its own.
  4. Run It from Anywhere (Apprentice): A script that reads its files from $PSScriptRoot works from any folder; a Scripts folder on your PATH lets you run it by name, pwsh -File starts it from outside PowerShell, and a desktop shortcut with pwsh -File in its target runs it with a double-click.

Exercise: A Morning Start-Up Script

  1. Exercise: A Morning Start-Up Script (Apprentice): Write morning.ps1, a script that greets whoever runs it, shows the date, reads the to-do list beside it from any folder and shows the free space on drive C, answer three questions about running it, and let a Pester check run it the way someone else would.