liveBook · Manning

Command

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

A command is an executable instruction that a shell can invoke to perform an operation.

A command is an executable instruction that a shell invokes to perform an operation. In PowerShell, you type a command, add parameters, press Enter, and immediately inspect the result. This interactive pattern lets administrators accomplish useful work without first writing a script. section 4.0 section 4.2

A PowerShell command commonly contains these parts:

  • A command or cmdlet name, commonly written in verb-noun form, such as Get-Command. section 4.3
  • Parameter names beginning with a dash, such as -Verb or -Module. section 4.3
  • Parameter values, which may be comma-separated; switch parameters need no value. section 4.3
  • Required spaces between the command, parameters, and values; capitalization does not matter, but accurate punctuation and spacing prevent avoidable errors. section 4.3

Finding the right command is part of performing the task. Start with PowerShell’s help system when you do not know what to use, when a command reports an error, or when you need to connect commands into a larger operation. Use Get-Help and Get-Command with wildcards to narrow possibilities, then read the examples and full help. section 3.1 section 7.6

Inspecting local disk space

Get-CimInstance -class Win32_LogicalDisk -computername localhost -filter "drivetype=3" | Sort-Object -property DeviceID | Format-Table -property DeviceID, @{label='FreeSpace(MB)';expression={$_.FreeSpace / 1MB -as [int]}}, @{label='Size(GB)';expression={$_.Size / 1GB -as [int]}}, @{label='%Free';expression={$_.FreeSpace / $_.Size * 100 -as [int]}} This command retrieves local logical disks, filters for fixed drives, sorts them, and formats free space, size, and percentage-free values. Long commands can be formatted across lines in VS Code while remaining one command. section 19.2

Get-CimInstance -class Win32_LogicalDisk -computername localhost `
-filter "drivetype=3" | Sort-Object -property DeviceID |
Format-Table -property DeviceID,
@{label='FreeSpace(MB)';expression={$_.FreeSpace / 1MB -as [int]}},
@{label='Size(GB)';expression={$_.Size / 1GB -as [int]}},
@{label='%Free';expression={$_.FreeSpace / $_.Size * 100 -as [int]}}

Use the complete disk-space command as a copyable example of combining a command, parameters, filtering, sorting, formatting, and calculated expressions. section 19.2

Commands can become repeatable automation. After repeatedly refining an interactive command, place it in a .ps1 file and run the script instead of retyping it. In VS Code, full command and parameter names make the resulting automation easier to read, test, and maintain. section 4.2 section 19.2

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains interactive execution, syntax anatomy, discovery, and repetition, while stopping short of comprehensive scripting internals.
Examples
Demonstrates command discovery, YAML module commands, and a CIM-based disk-space report tested in VS Code.
What it assumes
Assumes only basic shell familiarity and teaches command execution, parameters, syntax, discovery, and repetition from the beginning.
What you can do after
Apply commands interactively, discover unfamiliar commands with help, and convert reliable command lines into scripts.
How it connects
Connects commands with shells, cmdlets, parameters, modules, help systems, pipelines, remoting, and scripting.

Also explained in

Compare how 5 books explain command →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.