This lesson introduces PowerShell variables, constants, and data types.

Objectives and Skills edit

After completing this lesson, you will be able to:

  • Explain basic concepts regarding variables.
  • Describe the difference between variables and constants.
  • Understand data types and type conversions.
  • Use variables to capture user input and use that input later in script output.

Readings edit

  1. Wikipedia: Variable (computer science)
  2. Wikipedia: Constant (computer programming)
  3. Wikipedia: Data type
  4. Wikipedia: Type conversion
  5. Wikipedia: Scope (computer science)
  6. BonusBits: Mastering PowerShell Chapter 3 - Variables

Multimedia edit

  1. YouTube: PowerShell - Working with Variables
  2. YouTube: PowerShell - How To - Variables

Examples edit

Data Types edit

PowerShell data types include integers, floating point values, strings, Booleans, and datetime values.[1] The GetType method returns the current data type of the given variable.[2]

# This script demonstrates the difference between numeric and string data types.

$value = 1 + 1
$value                # Displays 2
$value.GetType()      # Displays Int32

$value = '1' + '1'
$value                # Displays 11
$value.GetType()      # Displays String

Type Conversion edit

Variables may be cast from one type to another by explicit conversion.

# This script demonstrates data types and data type conversion.

$value = 1.9

$value              # Displays 1.9
[int32]$value       # Displays 2
[float]$value       # Displays 1.9
[string]$value      # Displays 1.9
[boolean]$value     # Displays True
[datetime]$value    # Displays January 9 ...

Quotes edit

Single quotes display literal strings as is, without interpretation. Double quotes evaluate strings before displaying them.[3]

# This script demonstrates the difference between single quotes and double quotes.

$single = '$(1 + 2)'
$double = "$(1 + 2)"

Write-Output $single    # Displays $(1 + 2)
Write-Output $double    # Displays 3

Read-Host edit

The Read-Host cmdlet reads a line of input from the console.[4] Following is an example of a script that requests input from the user and then uses that input to generate the output. Note the use of both single and double quotes for string processing.

# This script displays a message based on user input.

$myinput = Read-Host 'Is it morning, afternoon, or evening? '
Write-Output "Good $myinput!"

Activities edit

  1. Review Microsoft TechNet: Get-Variable and Microsoft TechNet: about_Automatic_Variables. Start a new PowerShell session and use Get-Variable to display a list of all automatic variables and values that are defined by default when a new session is started.
  2. Review Microsoft TechNet: Using the Read-Host Cmdlet. Write a script that asks the user to enter their name, and then display a greeting back to the user that includes their name, such as 'Hello Wikiversity!'. Add a comment at the top of the script that describes the purpose of the script.
  3. Review Microsoft TechNet: about_Quoting_Rules. Experiment with the Hello script above using both using single quotes and double quotes to display the strings to ensure that you understand the difference between the two.
  4. Review Windows IT Pro: Working with PowerShell's Data Types. Experiment with entering different types of data (integers, floating point values, dates, strings) and use GetType() to display the data type entered.
  5. Review PowerShell.cz: Explicit Type Casting Versus Strongly Typed Variables. Experiment with data type conversion (type casting) and strongly typed variables when entering different types of data.

Lesson Summary edit

  • A variable or scalar is a storage location and an associated symbolic name (an identifier) which contains some known or unknown quantity or information, a value.[5]
  • A constant is an identifier whose associated value cannot typically be altered by the program during its execution.[6]
  • Many programming languages employ a reserved value (often named null or nil) to indicate an invalid or uninitialized variable.[7]
  • In almost all languages, variable names cannot start with a digit (0–9) and cannot contain whitespace characters.[8]
  • A data type or simply type is a classification identifying one of various types of data, such as real, integer or Boolean, that determines the possible values for that type, the operations that can be done on values of that type; the meaning of the data; and the way values of that type can be stored.[9]
  • Type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another.[10]
  • The scope of a variable describes where in a program's text the variable may be used, while the extent (or lifetime) describes when in a program's execution a variable has a (meaningful) value.[11]
  • Scope can vary from as little as a single expression to as much as the entire program, with many possible gradations -- such as code block, function, or module -- in between. [12]
  • In PowerShell, a variable name always begins with a dollar sign ($).[13]
  • PowerShell variable names are not case sensitive.[14]
  • The assignment operator (=) sets a variable to a specified value. You can assign almost anything to a variable, even complete command results.[15]
  • PowerShell data types include integers, floating point values, strings, Booleans, and datetime values.
  • Variables may be converted from one type to another by explicit conversion, such as [int32]$value.
  • In Windows PowerShell, single quotes display literal strings as is, without interpretation. Double quotes evaluate strings before displaying them.[16]
  • The Read-Host cmdlet reads a line of input from the console.[17]

Key Terms edit

ASCII (American Standard Code for Information Interchange)
A character-encoding scheme originally based on the English alphabet that encodes 128 specified characters - the numbers 0-9, the letters a-z and A-Z, some basic punctuation symbols, some control codes that originated with Teletype machines, and a blank space - into 7-bit binary integers.[18]
bit
A binary digit, having only two possible values most commonly represented as 0 and 1.[19]
Boolean
A data type with only two possible values: true or false.[20]
byte
A unit of digital information in computing and telecommunications that most commonly consists of eight bits, representing the values 0 through 255.[21]
character
A unit of information that roughly corresponds to a symbol , such as in an alphabet in the written form of a natural language.[22]
floating-point
A method of representing an approximation of a real number in a way that can support a wide range of values based on a fixed number of significant digits which are scaled using an exponent.[23]
garbage collection
A form of automatic memory management in which the garbage collector attempts to reclaim memory occupied by objects that are no longer in use by the program.[24]
immutable
An object whose state or value cannot be modified after it is created.[25]
integer
A data type which represents some finite subset of whole numbers, such as -32,768 to 32,767.[26][27]
memory leak
Incorrect management of memory allocation by a program, resulting in a reduction of available memory for running applications.[28]
string
A data type which represents a sequence of characters, either as a literal constant or as some kind of variable.[29]
Unicode
A computing industry standard for the consistent encoding, representation and handling of text expressed in most of the world's writing systems, in which a single character may be represented by one, two, or four bytes, depending on the character set and encoding used.[30]

Review Questions edit

Enable JavaScript to hide answers.
Click on a question to see the answer.
1. A variable or scalar is _____.
A variable or scalar is a storage location and an associated symbolic name (an identifier) which contains some known or unknown quantity or information, a value.
2. A constant is _____.
A constant is an identifier whose associated value cannot typically be altered by the program during its execution.
3. Many programming languages employ a reserved value (often named _____) to indicate an invalid or uninitialized variable.
Many programming languages employ a reserved value (often named null or nil) to indicate an invalid or uninitialized variable.
4. In almost all languages, variable names cannot start with _____ and cannot contain _____.
In almost all languages, variable names cannot start with a digit (0–9) and cannot contain whitespace characters.
5. A data type or simply type is _____ that determines _____, _____, _____, and _____.
A data type or simply type is a classification identifying one of various types of data, such as real, integer or Boolean, that determines the possible values for that type, the operations that can be done on values of that type; the meaning of the data; and the way values of that type can be stored.
6. Type conversion, typecasting, and coercion are _____.
Type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another.
7. The scope of a variable describes _____, while the extent (or lifetime) describes _____.
The scope of a variable describes where in a program's text the variable may be used, while the extent (or lifetime) describes when in a program's execution a variable has a (meaningful) value.
8. Scope can vary from as little as _____ to as much as _____, with many possible gradations -- such as _____ in between.
Scope can vary from as little as a single expression to as much as the entire program, with many possible gradations -- such as code block, function, or module -- in between.
9. In PowerShell, a variable name always begins with _____.
In PowerShell, a variable name always begins with a dollar sign ($).
10. PowerShell variable names _____ case sensitive.
PowerShell variable names are not case sensitive.
11. The assignment operator _____. You can assign almost anything to a variable, even _____.
The assignment operator (=) sets a variable to a specified value. You can assign almost anything to a variable, even complete command results.
12. PowerShell data types include _____.
PowerShell data types include integers, floating point values, strings, Booleans, and datetime values.
13. Variables may be converted from one type to another by _____.
Variables may be converted from one type to another by explicit conversion, such as [int32]$value.
14. In Windows PowerShell, single quotes display literal strings _____. Double quotes _____.
In Windows PowerShell, single quotes display literal strings as is, without interpretation. Double quotes evaluate strings before displaying them.
15. The Read-Host cmdlet _____.
The Read-Host cmdlet reads a line of input from the console.

Assessments edit

See Also edit

References edit

  Type classification: this is a lesson resource.
  Completion status: this resource is considered to be complete.