PowerShell/Variables
This lesson introduces PowerShell variables, constants, and data types.
Objectives and Skills
editAfter 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
editMultimedia
editExamples
editData Types
editPowerShell 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
editVariables 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
editSingle 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
editThe 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- 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
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
editAssessments
edit- Flashcards: Quizlet: Windows PowerShell - Variables
- Quiz: Quizlet: Windows PowerShell - Variables
- Practice Tests: Variables
- Flashcards: Quizplus: Variables
See Also
editReferences
edit- ↑ 4sysops: The PowerShell variable – Naming, value, data type
- ↑ Microsoft Developer Network: Object.GetType Method
- ↑ Microsoft TechNet: about_Quoting_Rules
- ↑ Microsoft TechNet: Read-Host
- ↑ Wikipedia: Variable (computer science)
- ↑ Wikipedia: Constant (computer programming)
- ↑ Wikipedia: Variable (computer science)
- ↑ Wikipedia: Variable (computer science)
- ↑ Wikipedia: Data type
- ↑ Wikipedia: Type conversion
- ↑ Wikipedia: Variable (computer science)
- ↑ Wikipedia: Scope (computer science)
- ↑ PowerShell.com: Master-PowerShell | With Dr. Tobias Weltner - Chapter 3. Variables
- ↑ PowerShell.com: Master-PowerShell | With Dr. Tobias Weltner - Chapter 3. Variables
- ↑ PowerShell.com: Master-PowerShell | With Dr. Tobias Weltner - Chapter 3. Variables
- ↑ Microsoft TechNet: about_Quoting_Rules
- ↑ Microsoft TechNet: Read-Host
- ↑ Wikipedia: ASCII
- ↑ Wikipedia: Bit
- ↑ Wikipedia: Boolean
- ↑ Wikipedia: Byte
- ↑ Wikipedia: Character (computing)
- ↑ Wikipedia: Floating point
- ↑ Wikipedia: Garbage collection (computer science)
- ↑ Wikipedia: Immutable object
- ↑ Wikipedia: Integer
- ↑ Wikipedia: Integer (computer science)
- ↑ Wikipedia: Memory leak
- ↑ Wikipedia: String (computer science)
- ↑ Wikipedia: Unicode