Course Overview

/

Powershell-Strings, Integers & Variables

Powershell-Strings and Integers

Powershell is a command line interface and scripting language used by Windows. Because of this, there is no need to install anything when working on a Windows operating system.

When creating a PowerShell script it is possible to either create the code in PowerShell itself by creating a file and executing it or using the PowerShell ISE (Integrated Scripting Environment). This is a tool designed to make scripting and debugging in PowerShell easier and is recommended when learning Powershell.

Starting our first script

Our first script will explore Strings and integers. These are important terms in coding and scripting, they refer to data types that can be used throughout a script.

A string is a sequence of characters, these can be either alphabetic, numerical, or special characters. In PowerShell, strings are enclosed in quotation marks when on a single line, both single quotes (‘) or double quotes (“) can be used. If a string is to be output onto multiple lines the @” and ”@ tags are used to encapsulate the string.

An integer is a whole number that can be either positive or negative. In PowerShell it is not necessary to enclose integers in any form of quotation marks.

1st Powershell Script

The first powershell script created is:

This will use the inbuilt PowerShell Write-Host function that will output the results of each command onto the command line.

As with our other scripting languages, PowerShell has the ability to conduct mathematical calculations as part of the script. These are encapsulated in parentheses and the $ character is used to represent the output of the calculation. The Write-Host command then outputs the value assigned to $ to the command line.

This is very similar when dealing with variables, the $ character is used to denote that x and y are variables that will be assigned a value. When outputting the final line of this script in PowerShell, make note that the variables are able to be directly referenced within the string. This is similar to our formatted strings in Python.