liveBook · Manning

Atomic type

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

A data type defines the kind of value represented and the operations applicable to that value.

A data type tells PowerShell what kind of value a variable contains and which operations apply. Input from Read-Host arrives as text, so multiplying "100" by 10 repeats the string instead of calculating 1,000. Declaring [int] before a variable makes PowerShell convert the input and reject values that are not valid integers. section 16.6

PS > $number = Read-Host "Enter a number"
Enter a number: 100
PS > $number = $number * 10
PS > $number
100100100100100100100100100100

Run the command to see how treating input as a string produces repeated text rather than numeric multiplication. section 16.6

Use type declarations and operators to control values:

  • [int], [single], and [double] represent numbers; [string] represents characters; [char] represents one character; and [xml] validates and parses XML content. section 16.6
  • Inspect a value with Get-Member to confirm its runtime type and available members. section 16.6
  • Use -as to attempt conversion, and use -is to test whether an object already has a particular type. section 26.2.1
  • Apply conversion inside formatted output, such as turning process memory from megabytes with decimals into whole numbers. section 11.6

Typed input

Declare [int]$number before Read-Host, then inspect it with Get-Member and multiply it. Entering 100 produces 1000; entering Hello raises a conversion error. A typed variable continues enforcing its type until explicitly retyped. section 16.6

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

How deep it goes
Explains declarations, runtime inspection, conversion, testing, and enforcement, while stopping short of deeper type-system internals.
Examples
Demonstrates numeric input, Get-Member inspection, invalid conversion, -as and -is tests, and process-memory formatting.
What it assumes
Assumes familiarity with variables and shell commands while demonstrating type behavior from the input stage.
What you can do after
Diagnose and prevent value-type errors with Get-Member, declarations, conversion, and type tests.
How it connects
Connects data types with variables, type conversion, operators, formatted output, and script parameters.

Also explained in

Compare how 17 books explain atomic type →

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.