Fundamentals of Operations

Introduction

An expression is a combination of two or more items. As a result, an expression can consist of performing an operation one two or more values.

The Addition

In arithmetic, the addition consists of adding one number to another. This operation is performed with the + symbol that separates two values. You can type the expression at a PowerShell command prompt or in the Visual Studio Code terminal. Here is an example:

New Project

The Subtraction

The subtraction consists of taking out one value from another. This operation is performed with the - symbol that separates two values. You can type the expression at a PowerShell command prompt or in the Visual Studio Code terminal. Here is an example:

PS C:\Windows\System32> 29731 - 8273
21458

The Multiplication

The multiplication consists of adding a certain value to itself a certain number of times. This operation is performed with the * symbol that separates two values. You can type the expression at a PowerShell command prompt or in the Visual Studio Code terminal. Here is an example:

PS C:\Windows\System32> 824 * 17
14008

The Division

The division consists of fracting a value a certain number of times. This operation is performed with the / symbol that separates two values. You can type the expression at a PowerShell command prompt or in the Visual Studio Code terminal. Here is an example:

PS C:\Windows\System32> 29731 / 83
358.204819277108

Fundamentals of Strings

Introduction

A string is a symbol or a combination of characters. To make the symbol or combination to be treated as a string, the character or combination must be included in single or double-quotes. Here are examples:

"a"

"xyz"

"29731 - 8273"

To prepare a string for PowerShell, you can type it at the PowerShell prompt or a Visual Studio Code terminal and press Enter. Here are examples:

PS C:\Windows\System32> "a"
a
PS C:\Windows\System32> "xyz"
xyz
PS C:\Windows\System32> "29731 / 8273"
29731 / 8273

String Addition

As seen with numbers, you can perform the addition operation on strings. Here are examples:

PS C:\Windows\System32> "Michael"
Michael
PS C:\Windows\System32> "Carlock"
Carlock
PS C:\Windows\System32> "Michael " + "Carlock"
Michael Carlock
PS C:\Windows\System32> "Paul" + " " + "Busbey"
Paul Busbey
PS C:\Windows\System32> "William" + " " + "Jefferson" + " " + "Clinton"
William Jefferson Clinton

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 shold use a combination of letters that clearly mean something. Examples are:

$age

$salary

$state

$firstName

$dateOfBirth

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:\Windows\System32> $hourlySalary = 22.37
PS C:\Windows\System32> $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:\Windows\System32> $a = 48
PS C:\Windows\System32> $hourlySalary = 22.37
PS C:\Windows\System32> $distance = 3175
PS C:\Windows\System32> $gender = "m"
PS C:\Windows\System32> $country = 'Zimbabwe'
PS C:\Windows\System32> $state = "North Carolina"

Primary Operations on Variables

Introduction to Value Display

Probably the primary operation of a program is to display something on the computer screen. There are various ways this can be done. If you want to simply display a number, at the PowerShell prompt or in the Visual Studio Code terminal, type that number and press Enter. Here are examples:

PS C:\Windows\System32> 8
8
PS C:\Windows\System32> 25
25
PS C:\Windows\System32> 582064
582064

If you want to display something that is not a number (a character, a group of characters, or any combination of symbols), you have many options. One solution is to include the character or combination in single or double-quotes. Here are examples:

PS C:\Windows\System32> 'New Jersey'
New Jersey
PS C:\Windows\System32> "Bavaria"
Bavaria
PS C:\Windows\System32> 'Mannitoba'
Mannitoba

Get the Value of a Variable

As seen in previous sections, one of the most common ways to use a value in an application is by declaring and variable and giving a value to it. We already had an introduction to variable declaration and initialization. Here are examples:

PS C:\Windows\System32> $firstName = "Michael"
PS C:\Windows\System32> $lastName = "Carlock"

After declaring a variable and once it has a value, to display that value, on the PowerShell prompt or in the Visual Studio Code terminal, type the name of the variable and press Enter. Here are examples:

PS C:\Windows\System32> $firstName = "Michael"
PS C:\Windows\System32> $lastName = "Carlock"
PS C:\Windows\System32> $firstName
Michael
PS C:\Windows\System32> $lastName
Carlock

Operations on Variables

Once you have declared a variable and have given it a value, the variable can be treated like a value. This means that you can perform any operation related to the type of value of the variable. If the variable is hoding a number value, you can add a value to it, you can subtract a value from it, you can multiply it by a value, or you can divide it. Here are examples:

PS C:\Windows\System32> $hourlySalary = 24.82
PS C:\Windows\System32> $hourlySalary * 40
992.8
PS C:\Windows\System32> $weeklyPay = $hourlySalary * 40
PS C:\Windows\System32> $weeklyPay
992.8

You can also perform such operations on two or more variables. Here are examples:

PS C:\Windows\System32> $monday = 7.5
PS C:\Windows\System32> $tuesday = 6
PS C:\Windows\System32> $wednesday = 8.5
PS C:\Windows\System32> $thursday = 6.5
PS C:\Windows\System32> $friday = 7
PS C:\Windows\System32> $timeWorked = $monday + $tuesday + $wednesday + $thursday + $friday
PS C:\Windows\System32> $weeklyPay = $hourlySalary * $timeWorked
PS C:\Windows\System32> "Time Worked: " + $timeWorked
Time Worked: 35.5
PS C:\Windows\System32> "Weekly Pay: " + $weeklyPay
Weekly Pay: 881.11

Writing to the Host

PowerShell provides a better mechanism to let you dispplay a value. To do this, 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:\Windows\System32> Write-Host 'Payroll Preparation'
Payroll Preparation
PS C:\Windows\System32> Write-Output "Employee Record"
Employee Record

If you want to display the value of a variable, you must use the double-quotes and not the single-quotes after Write-Host. Within the double-quotes, include the intended variable. In the same way, you can include more than one variable within the double-quotes. Here are examples:

PS C:\Windows\System32> Write-Host "First Name: $firstName"
First Name: Michael
PS C:\Windows\System32> Write-Host "Last Name: $lastName"
Last Name: Carlock
PS C:\Windows\System32> Write-Host "Full Name: $firstName $lastName"
Full Name: Michael Carlock

As mentioned previously, you can use the single-quotes on 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:\Windows\System32> Write-Output 'First Name:' $firstName
First Name: Michael
PS C:\Windows\System32> Write-Host 'Last Name:' $lastName
Last Name: Carlock
PS C:\Windows\System32> Write-Host 'Full Name:' $firstName $lastName
Full Name: Michael Carlock

Sometimes you want to perform operations on variables. As seen previously, you can perform such an operation and assign it to a variable. If you are not planning to use the value of such an operation many times, you can perform it on a Write-Host that would immediately display the result. In this case, you must write the operation outisde the quotes of Write-Host. The operation should be included inside some parentheses. Here are examples:

PS C:\Windows\System32> $mon = 7.5
PS C:\Windows\System32> $tue = 9
PS C:\Windows\System32> $wed = 10.5
PS C:\Windows\System32> $thu = 8.5
PS C:\Windows\System32> $fri = 8
PS C:\Windows\System32> $sal = 27.97
PS C:\Windows\System32> Write-Host 'Time Worked:' ($mon + $tue + $wed + $thu + $fri)
Time Worked: 43.5
PS C:\Windows\System32> Write-Host 'Weekly Salary:' (($mon + $tue + $wed + $thu + $fri) * $sal)
Weekly Salary: 1216.695

Writing an Output

In the previous section, we saw that you can put a value or a variable inside or outside a the quotes of Write-Host. The content of Write-Host and the variable would display on the same line. Here are examples:

PS C:\Windows\System32> $customer = "Thomas Stones"
PS C:\Windows\System32> Write-Host "Customer: $customer"
Customer: Thomas Stones
PS C:\Windows\System32> Write-Host "Customer:" $customer
Customer: Thomas Stones

Besides Write-Host, PowerShell also provides Write-Output to display something on the computer screen 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:\Windows\System32> $customer = "Thomas Stones"
PS C:\Windows\System32> Write-Host "Customer: $customer"
Customer: Thomas Stones
PS C:\Windows\System32> Write-Host "Customer:" $customer
Customer: Thomas Stones
PS C:\Windows\System32> Write-Output "Customer: $customer"
Customer: Thomas Stones
PS C:\Windows\System32> 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:\Windows\System32> $firstName = "Katherine"
PS C:\Windows\System32> $lastName  = "Crawford"
PS C:\Windows\System32> "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:\Windows\System32> $firstName = "Katherine"
PS C:\Windows\System32> $lastName  = "Crawford"
PS C:\Windows\System32> "Full Name: $firstName $lastName"
Full Name: Katherine Crawford
PS C:\Windows\System32> $lastName  = "Anderson"
PS C:\Windows\System32> "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:\Windows\System32> $employeeNumber = 974820
PS C:\Windows\System32> $gender = 'M'
PS C:\Windows\System32> $hourlySalary = 24.92
PS C:\Windows\System32> Write-Host "Employee #: " $employeeNumber
Employee #:  974820
PS C:\Windows\System32> Write-Host "Gender: " $gender
Gender:  M
PS C:\Windows\System32> 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 type of values. This means that, with this approach, you can assign a sertain 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:\Windows\System32> $employeeNumber = 974820
PS C:\Windows\System32> $gender = 'M'
PS C:\Windows\System32> $hourlySalary = 24.92
PS C:\Windows\System32> Write-Host "Employee #: " $employeeNumber
Employee #:  974820
PS C:\Windows\System32> Write-Host "Gender: " $gender
Gender:  M
PS C:\Windows\System32> Write-Host "Hourly Salary: " $hourlySalary
Hourly Salary:  24.92
PS C:\Windows\System32> $employeeNumber = "974,820"
PS C:\Windows\System32> $gender = 'Male'
PS C:\Windows\System32> $hourlySalary = "Twenty Four, Ninety Two"
PS C:\Windows\System32> Write-Host "Employee #: " $employeeNumber
Employee #:  974,820
PS C:\Windows\System32> Write-Host "Gender: " $gender
Gender:  Male
PS C:\Windows\System32> Write-Host "Hourly Salary: " $hourlySalary
Hourly Salary:  Twenty Four, Ninety Two

Characters and Strings for Variable

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 = variable-value

Based on this formula, start with some square brackets []. Add the $ symbol, a name for the variable, and assign a value to it.

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 then assign single or double-quotes to the variable. Inside the quotes, provide only one symbol. Here is an example:

PS C:\Windows\System32> [char]$gender = 'M'
PS C:\Windows\System32> 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:\Windows\System32> [string]$firstName = "Katherine"
PS C:\Windows\System32> [string]$lastName  = "Anderson"
PS C:\Windows\System32> [string]$fullName  = $firstName + " " + $lastName
PS C:\Windows\System32> Write-Host "First Name:" $firstName
First Name: Katherine
PS C:\Windows\System32> Write-Host "Last Name:" $lastName
Last Name: Anderson
PS C:\Windows\System32> Write-Host "Full Name:" $fullName
Full Name: Katherine 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. For such a variable, assign it 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. When dealing with such a variable, you can assign it almost any type of natural number.

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 o r 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

Single-Precision Numbers

A floating-point number is a number made of a natural section followed by a fraction of 1. The natural side of 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.

Double-Precision Numbers

If you want a variable to hold a floating-point number with much precision, specify its data type as double.

Decimal Numbers

If you want a variable to hold a very large floating-point number, specify its data type as decimal.

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. For such a variable, assign it a value between 0 and 255.

Other Topics on Writing Code

Ending a Statement

In many traditional computer languages, to end a statement, you must type a sami-colon. This is also allowed in PowerShell; but it PowerShell, the semi-colon that ends a statement is option. This means that you can end a statement with a semi-colon or you can omit it.

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 caculation. As a result, you can write anything ou 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 file according to the operating system). 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 processing
$fullName = $firstName + ' ' + $lastName
$timeWorked = $mon + $tue + $wed + $thu + $fri
$netPay = $timeWorked * $salary

# Payroll Summary
Write-Host 'Employee #:   ' $emplNumber
Write-Host 'Employee Name:' $fullName 
Write-Host 'Hourly Salary:' $salary
Write-Output '--------------------------------'
Write-Output 'Time Worked:  ' $timeWorked
Write-Output 'Weekly Salary:' $netPay 

After creating a PowerShell file, you can execute from either the PowerShell prompt or Visual Studio Code. 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\Introduction.ps1
Employee #:    497394
Employee Name: Michael Carlock
Hourly Salary: 27.97
--------------------------------
Time Worked:   43.5
Weekly Salary: 1216.695

Previous Copyright © 2024-2025, FunctionX Monday 02/03/2025, 21:10 Next