# PowerShell/Variables

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

## Objectives and Skills

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

* Wikipedia: Variable (computer science)
* Wikipedia: Constant (computer programming)
* Wikipedia: Data type
* Wikipedia: Type conversion
* Wikipedia: Scope (computer science)
* BonusBits: Mastering PowerShell Chapter 3 - Variables

## Multimedia

* YouTube: PowerShell - Working with Variables
* YouTube: PowerShell - How To - Variables

## Examples
#### Data Types

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

#### Type Conversion

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

#### Quotes

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

#### Read-Host

The Read-Host cmdlet reads a line of input from the console. 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.

## Activities

* 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.
* 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.
* 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.
* 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.
* 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

* 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.
* A constant is an identifier whose associated value cannot typically be altered by the program during its execution.
* Many programming languages employ a reserved value (often named null or nil) to indicate an invalid or uninitialized variable.
* In almost all languages, variable names cannot start with a digit (0–9) and cannot contain whitespace characters.
* 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.
* Type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another.
* 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.
* 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.
* In PowerShell, a variable name always begins with a dollar sign ($).
* PowerShell variable names are not case sensitive.
* The assignment operator (=) sets a variable to a specified value. You can assign almost anything to a variable, even complete command results.
* 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.
* The Read-Host cmdlet reads a line of input from the console.

## Key Terms
### 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.

### bit

    A binary digit, having only two possible values most commonly represented as 0 and 1.

### Boolean

    A data type with only two possible values: true or false.

### byte

    A unit of digital information in computing and telecommunications that most commonly consists of eight bits, representing the values 0 through 255.

### character

    A unit of information that roughly corresponds to a symbol , such as in an alphabet in the written form of a natural language.

### 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.

### 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.

### immutable

    An object whose state or value cannot be modified after it is created.

### integer

    A data type which represents some finite subset of whole numbers, such as -32,768 to 32,767.

### memory leak

    Incorrect management of memory allocation by a program, resulting in a reduction of available memory for running applications.

### string

    A data type which represents a sequence of characters, either as a literal constant or as some kind of variable.

### 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.

## Assessments

* Flashcards: Quizlet: Windows PowerShell - Variables
* Quiz: Quizlet: Windows PowerShell - Variables
* Practice Tests: Variables
* Flashcards: Quizplus: Variables