Introduction to Variables
Introduction to Variables
Fundamentals of Variables
Introduction
A variable is a value that is stored in the computer memory. You, as the programmer, has to inform PowerShell that you want to store a value in the computer memory. In turn, the computer needs two required pieces of information and one optional piece of information. The first required piece is to let the computer know that you will use a variable. To do that, you start with the $ symbol.
A Name for a Variable
The second required piece of information the computer needs about a variable is a name. That name must be written immediately after the $ symbol. A name can be made of a single letter. Examples are:
$a $m $x
To make it clearer, the name of a variable should be explicit. In this case, the name should use a combination of letters that clearly mean something. By tradition, if the name represents one word, that name should be in lowercase. Here are examples:
$age $salary $state
If the name is a combination of words, you can separate them with underscores. Here are examples:
$first_name $last_name $date_of_birth
As another technique, the first word should be in lowercase, then the first letter of each subsequent word should be in uppercase while the other letters are in lowercase. Here are examples:
$employeeName $countryOfOrigin
Initializing a Variable
When declaring a variable, the optional piece of informtion that the computer needs is a primary value for a variable. To provide this information, after the name of a variable, type = followed by the desired value. The = symbol is referred to as the assignment operator. If the variable should hold a number, after =, type that number. Here are examples:
S C:\Windows\System32> $a = 48 PS C:\Exercises> $hourlySalary = 22.37 PS C:\Exercises> $distance = 3175
If the variable should hold a symbol or a combination words, include that symbol or combination in single or double-quotes. Here are examples:
PS C:\Exercises> $a = 48 PS C:\Exercises> $hourlySalary = 22.37 PS C:\Exercises> $distance = 3175 PS C:\Exercises> $gender = "m" PS C:\Exercises> $country = 'Zimbabwe' PS C:\Exercises> $state = "North Carolina"
Displaying a Value
Introduction
In the previous lesson, we saw that, to display a value, you can type it on the PowerShell prompt or the Visual Studio terminal and press Enter. This is also possible with variable. You have many other options.
Displaying a Variable
To display the value of a variable, you have many options. At a minimum, after declaring and initializing a variable, to display its value, in the PowerShell prompt or the Visual Studio terminal, type the name of the variable, including its $ symbol, and press Enter. Here are examples:
PS C:\Windows\System32> $emplNbr = 927468 PS C:\Windows\System32> $fullName = "Jonathan Simms" PS C:\Windows\System32> $sal = 24.73 PS C:\Windows\System32> $emplNbr 927468 PS C:\Windows\System32> $fullName Jonathan Simms PS C:\Windows\System32> $sal 24.73
A Variable in a String
Notice that, in the above examples, it is disfficult what the displayed value represents. As an alternative, you can create a sentence and include the name of the variable, including its $ symbol, in the string. The string must be in double-quotes. Here are examples:
PS C:\Windows\System32> $emplNbr = 927468 PS C:\Windows\System32> $fullName = "Jonathan Simms" PS C:\Windows\System32> $sal = 24.73 PS C:\Windows\System32> $emplNbr 927468 PS C:\Windows\System32> $fullName Jonathan Simms PS C:\Windows\System32> $sal 24.73 PS C:\Windows\System32> "Employee #: $emplNbr" Employee #: 927468 PS C:\Windows\System32> "Employee Name: $fullName" Employee Name: Jonathan Simms PS C:\Windows\System32> "Hourly Salary: $sal" Hourly Salary: 24.73
When using this technique, if you create the string with single-quotes, PowerShell would treat everything in the string as text, including the variable even if you include its $ symbol.
Writing to the Host
PowerShell provides other options to let you display a value. As one option, on the PowerShell prompt or in the Visual Studio Code terminal, type Write-Host. After Write-Host, you can include a string in single or double-quotes. Here are examples:
PS C:\Exercises> Write-Host 'Payroll Preparation' Payroll Preparation PS C:\Exercises> Write-Output "Employee Record" Employee Record
You can also use Write-Host to display the value of a variable. Once again, you have many options. As one technique, after Write-Host, type some double-quotes. Within the double-quotes, include the name of the variable, including its $ symbol. Inside the double-quotes, you can include any text you want, before or after the variable. Here are examples:
PS C:\Exercises> Write-Host "First Name: $firstName" First Name: Michael PS C:\Exercises> Write-Host "Last Name: $lastName" Last Name: Carlock
Inside the double-quotes, you can include as many variables and text pieces as you want. Here is an example:
PS C:\Exercises> Write-Host "Full Name: $firstName $lastName"
Full Name: Michael Carlock
As another technique, you can apply the single-quotes to Write-Host. In that case, if you want to display the value of a variable, you must write that variable outside the single-quotes. Here are examples:
PS C:\Exercises> Write-Output 'First Name:' $firstName First Name: Michael PS C:\Exercises> Write-Host 'Last Name:' $lastName Last Name: Carlock
In the same way, you can add as many variables as you want outisde the single-quotes. Here is an example:
PS C:\Exercises> Write-Host 'Full Name:' $firstName $lastName
Full Name: Michael Carlock
Writing an Output
In the previous section, we saw that you can put a value or a variable inside or outside the quotes (single or double) of Write-Host. The content of Write-Host and the variable would display on the same line. Here are examples:
PS C:\Exercises> $customer = "Thomas Stones" PS C:\Exercises> Write-Host "Customer: $customer" Customer: Thomas Stones PS C:\Exercises> Write-Host "Customer:" $customer Customer: Thomas Stones
Besides Write-Host, PowerShell provides Write-Output to display something on the computer screen. As seen with Write-Host, you can include a value or a variable in the double-quotes (not single-quotes) of Write-Output. In this case, the contents of Write-Output and the included value or variable would display on the same line. If you put a value or a variable outside the double-quotes of Write-Output, whatever is outside the double-quotes of Write-Output would display on the next line. Consider the following lines of code:
PS C:\Exercises> $customer = "Thomas Stones" PS C:\Exercises> Write-Host "Customer: $customer" Customer: Thomas Stones PS C:\Exercises> Write-Host "Customer:" $customer Customer: Thomas Stones PS C:\Exercises> Write-Output "Customer: $customer" Customer: Thomas Stones PS C:\Exercises> Write-Output "Customer:" $customer Customer: Thomas Stones
Topics on Variables
Updating a Variable
When writing your code, you can declare a new variable in any section of your code and use that variable as you see fit. Whenever you declare a variable and of course give it a name, PowerShell checks if there exists already a variable with that name. If there is no variable with that name, PowerShell considers that you have just declared a new variable with that name. Consider the following example:
PS C:\Exercises> $firstName = "Katherine" PS C:\Exercises> $lastName = "Crawford" PS C:\Exercises> "Full Name: $firstName $lastName" Full Name: Katherine Crawford
On the other hand, if you appear to declare a variable by using a name of an existing variable and assign a new value to it, PowerShell considers that you are simply changing the value of an existing variable. In that case, you are said to update a variable. Here is an example:
PS C:\Exercises> $firstName = "Katherine" PS C:\Exercises> $lastName = "Crawford" PS C:\Exercises> "Full Name: $firstName $lastName" Full Name: Katherine Crawford PS C:\Exercises> $lastName = "Anderson" PS C:\Exercises> "Full Name: $firstName $lastName" Full Name: Katherine Anderson
The Data Type of a Variable
You may remember that, when you declare a variable, the computer must set aside a certain size (and area) of its memory to hold the value of that variable. In traditional application programming, a data type is a name that indicates the size of the computer memory that is necessary to hold the value of a variable. As it happens, there are different names for the different sizes (and types) of memory used to hold the value of a variable.
As we have done so far, when you declare a variable in PowerShell, you don't have to indicate its type. You can just type $ followed by a letter or a combination of letters, digits, and underscores. Here are examples:
PS C:\Exercises> $employeeNumber = 974820 PS C:\Exercises> $gender = 'M' PS C:\Exercises> $hourlySalary = 24.92 PS C:\Exercises> Write-Host "Employee #: " $employeeNumber Employee #: 974820 PS C:\Exercises> Write-Host "Gender: " $gender Gender: M PS C:\Exercises> Write-Host "Hourly Salary: " $hourlySalary Hourly Salary: 24.92
One of the advantages (and disadvantages) of languages that allow this approach (PowerShell is not the only language that allows this technique) is that they allow a variable to hold different types of values. This means that, with this approach, you can assign a certain value to a variable at one time and then assign a different type of value to the same variable at another time. Here are examples:
PS C:\Exercises> $employeeNumber = 974820 PS C:\Exercises> $gender = 'M' PS C:\Exercises> $hourlySalary = 24.92 PS C:\Exercises> Write-Host "Employee #: " $employeeNumber Employee #: 974820 PS C:\Exercises> Write-Host "Gender: " $gender Gender: M PS C:\Exercises> Write-Host "Hourly Salary: " $hourlySalary Hourly Salary: 24.92 PS C:\Exercises> $employeeNumber = "974,820" PS C:\Exercises> $gender = 'Male' PS C:\Exercises> $hourlySalary = "Twenty Four, Ninety Two" PS C:\Exercises> Write-Host "Employee #: " $employeeNumber Employee #: 974,820 PS C:\Exercises> Write-Host "Gender: " $gender Gender: Male PS C:\Exercises> Write-Host "Hourly Salary: " $hourlySalary Hourly Salary: Twenty Four, Ninety Two
A Variable for One or more Characters
Introduction
PowerShell allows you to indicate the type of value that a variable must have. The formula to indicate the data type of a variable is:
[type-name]$variable-name
Based on this formula, start with some square brackets: [ and ]. Inside those brackets, enter the desired type. Outside the brackets, add the $ symbol and a name for the variable. Optionally, assign a value to the variable.
A Character
An application uses various types of symbols such as alphabetical characters, digits, punctuation signs, etc. To indicate that a variable can hold any type of symbol, specify its data type as char. You can optionally assign single or double-quotes to the variable. If you decide to initialize the variable, inside the quotes, provide only one symbol. Here is an example:
PS C:\Exercises> [char]$gender = 'M'
PS C:\Exercises> Write-Host "Gender: " $gender
Gender: M
If you put more than one character in the quote, PowerShell would produce an error.
A String
A string is one or a combination of symbols (alphabetical characters, digits, punctuation signs, etc). To indicate that a variable will use a string, specify its data type as string. You can then assign single or double-quotes to the variable. Inside the quotes, provide as many symbols as you want. Here is an example:
PS C:\Exercises> [string]$firstName = "Katherine" PS C:\Exercises> [string]$lastName = "Anderson" PS C:\Exercises> Write-Host "First Name:" $firstName First Name: Katherine PS C:\Exercises> Write-Host "Last Name:" $lastName Last Name: Anderson
Natural Numbers
A Byte
A byte is a small number between 0 and 255. To indicate that a variable must hold such a small number, specify its data type as byte. Whenever you decide to assign a value to the variable, specify a value between 0 and 255.
A Signed Byte
To indicate that a variable can hold a small negative or positive number between -128 and 127, specify its data type as sbyte.
A Short Integer
A natural number is referred to as short if it is somewhere between -32768 and 32767. To indicate that a variable must hold such a number, specify its data type as short.
A Unsigned Short Integer
To indicate that a variable can hold a relatively small positive number between 0 and 65535, specify its data type as ushort.
An Integer
A natural number is also called an integer. An integer is a natural number that can be negative or positive. For application programming purposes, an integer is a number between -2,147,483,648 and 2,147,484,647. To indicate that a variable must hold such a natural number, specify its data type as int.
An Unsigned Integer
A unsigned integer is a positive natural number between 0 and 4,294,967,295. To indicate that a variable must hold such a number, specify its data type as uint.
A Long Integer
A natural number is referred to as long if it can be very large. A long integer is a natural number that can be as low as -9,223,372,036,854,775,807, or the number can be as high as 9,223,372,036,854,775,807. If you want a variable to be able to hold a very small natural number or a very high natural number, specify its data type as long. You can then assign any natural number to the variable.
An Unsigned Long Integer
A unsigned long integer is a positive natural number between 0 and 18,446,744,073,709,551,615. To indicate that a variable can hold a very high positive number, specify its data type as ulong.
Floating-Point Numbers
A floating-point number is a number made of a natural section followed by a fraction of 1. The natural side and the fractional side are separaterd by a character referred to as a decimal separator. To indicate that a variable will hold a floating-point number but you are are not concerned with precision, specify its data type as float.
If you want a variable to hold a floating-point number with much precision, specify its data type as double.
If you want a variable to hold a very large floating-point number, specify its data type as decimal.
Other Topics on Writing Code
Code Comment
A comment is some text that will not be processed as part of your code. To create a comment, type # followed by any text you want. Anything that comes after # will not display or be involved in a calculation. As a result, you can write anything you want after the # symbol. Here is an example of a comment:
# This series of lessons introduces PowerShell programming.
A PowerShell File
So far, the primary way we used PowerShell was to write a line of code at the PowerShell command prompt or the Visual Studio Code terminal. If you have many lines of code to execute, you can continually type each line and press Enter. In some cases, you want to execute many lines of code as one unit or as a block. In that case, you can create a file that you would execute later.
A PowerShell file is primarily a text-based document like any other. You can create the file in a text editor like Notepad, in Visual Studio Code, etc. When saving the file, give it an appropriate name (a name that follows the rules of names of files based on the operating system you are using). Save the file with the .ps1 extension. Such a document becomes a PowerShell file. In that document, you can write one or more lines of PowerShell code. Here is an example of the contents of a PowerShell file:
# Employee identification $emplNumber = 497394 $firstName = 'Michael' $lastName = 'Carlock' # Hourly salary $salary = 27.97 # Time worked during a week $monday = 7.5 $tuesday = 9 $wednesday = 10.5 $thursday = 8.5 $friday = 8 # Payroll Summary Write-Output "================================" Write-Output 'Payroll Evaluation - Time Sheet' Write-Output "================================" Write-Output 'Employee Identification' Write-Output '--------------------------------' Write-Host 'Employee #: ' $emplNumber Write-Host 'Employee Name: ' $firstName $lastName Write-Host 'Hourly Salary: ' $salary Write-Output "================================" Write-Output 'Time Worked' Write-Output '--------------------------------' Write-Output "Monday: $monday" Write-Output "Tuesday: $tuesday" Write-Output "Wednesday: $wednesday" Write-Output "Thursdday: $thursday" Write-Output "Friday: $friday" Write-Output "================================"
After creating a PowerShell file, you can execute it from either the PowerShell prompt or Visual Studio Code terminal. To do that, at the PowerShell command prompt or the Visual Studio terminal, type the full name of the file with its extension and press Enter. Here is an example:
PS C:\Windows\System32> C:\Exercises\PayrollEvaluation1.ps1
================================
Payroll Evaluation - Time Sheet
================================
Employee Identification
--------------------------------
Employee #: 497394
Employee Name: Michael Carlock
Hourly Salary: 27.97
================================
Time Worked
--------------------------------
Monday: 7.5
Tuesday: 9
Wednesday: 10.5
Thursdday: 8.5
Friday: 8
================================
|
|||
Previous | Copyright © 2024-2025, FunctionX | Monday 02/03/2025, 21:10 | Next |
|