Let’s start with Powershell – part2
Be sure to check first part of this short powershell tutorial here
Variables
In this part I will show you the most common types of variables.
Variables are labels we use to store data that can vary (hence the name “variable”). In PowerShell variables are referenced and dereferenced by appending the variable name with $ symbol.
In PowerShell variables can be of many types:
- numbers (positive or negative, e.g. 1, 5 ,-17, 0)
- strings (sequences of characters that make, for example, words or sentences)
- arrays (sequences of variables referenced by an integer index, for example strings can be treated as arrays)
- hash tables (sometimes called dictionary, these are key value pairs)
- objects
(an object is may contain a complex set of variable types and associations) –
some examples include:
- processes
- services
- event logs
- computers
- XML
- anything you can think might need a variable!
Integers
Variables can handle numbers and perform arithmetic operations. There are several types of variable that can contain numbers. The simplest variable type is the [int] type for integers.
PS H:\> $a=3 PS H:\> $b=6 PS H:\> $c=$a+$b PS H:\> $c 9
Doubles
Variables of type [double] are similar to integers in that they hold numbers however the number can contain fractional parts as decimals
PS H:\> $b=3.3 PS H:\> write-host $b 3,3 PS H:\> $b.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Double System.ValueType
Strings
PS H:\> $text="Test Text" PS H:\> $text Test Text
Integers and strings
if we add the two variables, the output type will depend on the type of the first variable listed.
test
PS H:\> $a="3" PS H:\> $b=6 PS H:\> $c=$a+$b PS H:\> $c 36 $a – string $b – integer $c – string
Next Page…