liveBook · Manning

Integer

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

An integer is a whole number with no fractional or decimal portion.

An integer is a whole number with no fractional or decimal portion. In PowerShell, parameter help may label integer input as Int, Int32, or Int64. These labels tell you what kind of value a command expects. section 3.5.4

PowerShell can mistake user input for text. If a variable receives the string "100", multiplying it by 10 repeats the text instead of performing arithmetic. Declaring the variable with [int] forces integer conversion and makes the intended operation clear. section 16.6

Force numeric input

Use [int] before the variable when reading a number: [int]$number = Read-Host "Enter a number". Entering 100 produces a System.Int32 value, so $number * 10 returns 1000. Entering Hello raises a conversion error rather than silently storing unusable text. section 16.6

Use an integer as a command value

A variable can stand in for a numeric command argument. Assigning 13481 to $var and running Get-Process -Id $var retrieves the process with that identifier; Get-Member identifies the value as System.Int32. section 16.2

PS > [int]$number = Read-Host "Enter a number"         #1
Enter a number: 100
PS > $number | Get-Member
   TypeName: System.Int32                              #2
Name        MemberType Definition
----        ---------- ----------
CompareTo   Method     int CompareTo(System.Object value), int CompareT...
Equals      Method     bool Equals(System.Object obj), bool Equals(int ...
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
GetTypeCode Method     System.TypeCode GetTypeCode()
ToString    Method     string ToString(), string ToString(string format...
PS > $number = $number * 10
PS > $number
1000                                                   #3

Inspect the complete typed-input example, including [int] declaration, System.Int32 output, multiplication, and the resulting value. section 16.6

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

How deep it goes
Defines whole numbers and demonstrates typed input, runtime inspection, arithmetic, command arguments, and conversion failures.
Examples
Demonstrates Read-Host input, [int] variables, multiplication, conversion errors, Get-Member inspection, and Get-Process -Id.
What it assumes
Assumes basic PowerShell variables and commands while teaching string-versus-integer input behavior from the examples.
What you can do after
Apply it via PowerShell by declaring numeric variables, validating their runtime type, performing arithmetic, and supplying command identifiers.
How it connects
Connects integers with PowerShell variables, strings, type conversion, arithmetic, command parameters, and Get-Member.

Also explained in

Compare how 15 books explain integer →

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.