Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 34 ยท Getting Started

Installing and Your First Commands

Install PowerShell 7, choose between pwsh and powershell.exe, set up Windows Terminal or VS Code, and run first commands like Get-Date and Write-Output.

Before you can write scripts you need a prompt to type at. This lesson gets one on your machine, shows the difference between pwsh and powershell.exe, and walks through the first few commands everyone runs: asking for the date, printing text, finding out where you are and fixing a typo with history and Tab completion.

Installing software cannot happen in your browser, so the install steps below are read-only. Afterwards a JavaScript stand-in imitates a prompt so you can practice how commands, output and the prompt fit together. It is not a real PowerShell, and the lesson marks where it differs.

Which PowerShell do you already have?

On Windows 10 and 11, Windows PowerShell 5.1 is already installed. Search the Start menu for "Windows PowerShell" or run powershell from any terminal. On Linux and macOS nothing is preinstalled. If you read What Is PowerShell? you know that PowerShell 7 is the edition this tutorial teaches, so install it next to what you have.

Installing PowerShell 7

The commands below are typed in your terminal, not here.

POWERSHELL
# Windows: from Windows PowerShell or Command Prompt, using the Windows package manager
winget install --id Microsoft.PowerShell --source winget
Terminal
# macOS (Homebrew)
brew install --cask powershell

# Linux: install through your distribution's package manager once Microsoft's
# package repository is configured, or use the snap package
sudo snap install powershell --classic

The exact steps vary by distribution and change over time, so when you set up Linux, follow the current instructions from the PowerShell project for your distribution. After installing, start it with:

TEXT
pwsh

Your prompt changes to something like PS C:\Users\ada> on Windows or PS /home/ada> on Linux. The letters PS tell you it is a PowerShell prompt.

Program What it starts
powershell or powershell.exe Windows PowerShell 5.1 (Windows only)
pwsh PowerShell 7 (all platforms)

If you are unsure which window you have open, run $PSVersionTable.PSVersion. A major version of 5 means Windows PowerShell; 7 means PowerShell 7.

Every shell window is its own session. You can have 5.1 and 7 open at the same time, and a setting changed in one does not affect the other.

A good place to type: terminal and editor

PowerShell itself is only the engine. The window around it is the terminal.

  • Windows Terminal is a modern terminal for Windows with tabs, so you can keep PowerShell 7, Windows PowerShell and Command Prompt side by side. It is the default on recent Windows 11. It lets you choose which shell opens in a new tab.
  • Visual Studio Code with the PowerShell extension gives you a script editor with colors, completion, a debugger and an integrated terminal. It is the editor most people use for scripts.
  • On Linux and macOS, your usual terminal app works. Run pwsh in it, or make pwsh your login shell.

For one-liners the terminal is enough. When your code grows past a few lines, move it into a .ps1 file in an editor (Scripts and Execution Policy explains how to run it).

Your first commands

Type the command after the PS> prompt and press Enter. Everything below is genuine PowerShell.

POWERSHELL
Get-Date

Get-Date returns the current date and time as a DateTime object, which the shell shows as a line such as Saturday, March 2, 2024 2:30:00 PM. Because it is an object, you can ask for parts of it:

POWERSHELL
(Get-Date).Year
(Get-Date).AddDays(30)
Get-Date -Format "yyyy-MM-dd"

Printing text uses Write-Output:

POWERSHELL
Write-Output "Hello, PowerShell"

You often do not need it. Any value you leave unassigned in a line is sent to the output, so typing "Hello, PowerShell" alone prints the same thing. Other useful starters:

POWERSHELL
Get-Location        # where am I? (alias: pwd)
Set-Location ..     # go up one folder (alias: cd)
Get-ChildItem       # list files and folders (aliases: ls, dir)
Clear-Host          # clear the screen (alias: cls)
exit                # close this session

On Windows the aliases ls, dir, cat, cd and pwd point to PowerShell cmdlets. On Linux and macOS, PowerShell does not define ls or cat, so those names run the system programs instead. Using the full cmdlet name gives the same behavior everywhere.

The prompt, history and Tab

The prompt is produced by a function named prompt. You can see it with Get-Content Function:\prompt, and you will learn to customize it in Modules and Profiles.

Three habits make typing fast:

  • Up and Down arrows walk through previous commands. Get-History lists them for the session. PSReadLine, the line editor PowerShell uses, also saves history to a file between sessions.
  • Tab completes command names, parameter names, file paths and even enumerated values. Type Get-Chi and press Tab to get Get-ChildItem. Press Tab repeatedly to cycle through the options.
  • Ctrl+C stops a running command.

Try it: a pretend prompt

This stand-in is a toy command runner written in JavaScript. It does not run PowerShell. It mimics three ideas: each line starts with a prompt, a command produces output, and an unknown command produces an error message. The date is fixed so the result is always the same.

JS Stand-in: a tiny prompt that echoes commands

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real: the command names, the case-insensitive matching and the idea that an unrecognized name produces an error. What is the stand-in: the real shell parses far more (quotes, pipes, variables) and the real error message includes the line, position and suggestions.

Case, quotes and typos

PowerShell command and parameter names are not case sensitive: get-date, GET-DATE and Get-Date are the same command. Convention is to write the proper Get-Date form in scripts.

Text with spaces needs quotes. Single quotes keep the text exactly as written; double quotes expand variables (you will see this in Strings, Quoting and Here-Strings).

POWERSHELL
Write-Output 'Hello there'
Write-Output "Today is $(Get-Date -Format 'dddd')"

When you misspell a command, PowerShell prints an error with the unrecognized name. Check for a typo first and use Tab completion to avoid making it.

A second stand-in: Tab completion

Completion is a simple idea: given the letters you typed, list the commands that start with them. This stand-in does exactly that with a short list of real cmdlet names.

JS Stand-in: prefix completion over cmdlet names

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Real completion also fills in parameter names, values and file paths, and a Tab press picks the next match instead of printing them all. The prefix idea is the same.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You open a window and want to be sure it is PowerShell 7. What should you check?

    Both editions show a PS prompt, and Windows Terminal can host either one. The version table tells you the real answer.
  2. 2. Which command starts PowerShell 7 from another terminal?

    pwsh is the PowerShell 7 executable on all platforms. powershell starts Windows PowerShell 5.1 on Windows.
  3. 3. What does (Get-Date).AddDays(30) demonstrate?

    Get-Date returns a DateTime object. The parentheses run it first, then .AddDays(30) calls a method that returns a new DateTime. The system clock is untouched.
  4. 4. Which key completes Get-Chi to Get-ChildItem?

    Tab cycles through completions. Ctrl+C interrupts a running command.

Summary

  • Windows PowerShell 5.1 is preinstalled on Windows; install PowerShell 7 separately (for example with winget) and start it with pwsh.
  • Windows Terminal gives you tabs, and VS Code with the PowerShell extension is a good script editor.
  • Get-Date, Write-Output, Get-Location, Set-Location and Get-ChildItem are common first commands.
  • Names are not case sensitive; Tab completes them and the arrow keys recall history.
  • The install and shell commands are read-only here; the JavaScript blocks are stand-ins.

Next: Cmdlets and the Verb-Noun Pattern.