0% found this document useful (0 votes)
45 views

Module 3: Variables, Data Types and Array

This document discusses variables, data types, and arrays in PowerShell. It covers defining variables and assigning data types, built-in PowerShell data types, protecting and working with variables using cmdlets, environment variables, here strings, escape sequences, sub expressions, and the Get-Member cmdlet. It also discusses arrays, including creating and accessing elements, and hash tables for storing key-value pairs. Comparison and arithmetic operators are also covered.

Uploaded by

madan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Module 3: Variables, Data Types and Array

This document discusses variables, data types, and arrays in PowerShell. It covers defining variables and assigning data types, built-in PowerShell data types, protecting and working with variables using cmdlets, environment variables, here strings, escape sequences, sub expressions, and the Get-Member cmdlet. It also discusses arrays, including creating and accessing elements, and hash tables for storing key-value pairs. Comparison and arithmetic operators are also covered.

Uploaded by

madan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Module 3: Variables, Data types and Array

Variables/Data types:
 PowerShell assigns best fit primitive data type for a given value
 The process of automatic selection of data type is called “Weekly typed”
 Ex: $Course=‘PowerShell’, $Duration=2
 Process of specifying the exact type of data to be stored is called “Strongly Typed”
 Ex:[String]$Course=‘PowerShell’, [Int32]$Duration=2
 <Variable_Name>.GetType().Name – Gives the actual data type of variable
 Ex: [Byte]$Flag=12, $Flag.GetType().Name

Variable Type Description Example


[bool] Yes-no value [boolean]$flag = $true
[char] Individual unicode character [char]$a = "t"
Double-precision floating point
[double] $amount = 12.45
decimal
[int16] 16-bit integer with characters [int16]$value = 1000
[int32],
32-bit integers with characters [int32]$value = 5000
[int]
[int64],
64-bit integers with characters [int64]$value = 4GB
[long]
[single], Single-precision floating point
[single]$amount = 44.67
[float] number
[string] String [string]$text = "Hello"
[array] An array [array]$Students=("Peter","John","Ram","Rohan")

Working with Variables using cmd-let:


PS C:\Users\v-bapunn> New-Variable -Name a -Value "10"
PS C:\Users\v-bapunn> Get-Variable a
Name Value
---- -----
a 10
PS C:\Users\v-bapunn> Set-Variable -Name a 234
PS C:\Users\v-bapunn> Set-Variable -Name a -Value 234
PS C:\Users\v-bapunn> $a
234
PS C:\Users\v-bapunn> Get-Variable a
Name Value
---- -----
a 234

PS C:\Users\v-bapunn> Remove-Variable a

Apple Software Solutions Page 1


Protecting Variable:

Option Description
"None" NO option (default)
"ReadOnly” Variable contents may only be modified by means of the -force
parameter

"Constant" Variable contents can't be modified at all. This option must already
be specified when the variable is created. Once specified this option
cannot be changed.

"Private" The variable is visible only in a particular context (local variable).


"AllScope" The variable is automatically copied in a new variable scope.

Syntax: New-Variable –Name <Variable_Name> -Value <Variable_Value> -Option


<ReadOnly/Constant/Private/AllScope>

Environment Variables:
 “Dir Env:” to view all available environment variables
 $Env:Windir, $Env:ComputerName, $Env:UserDomain,Env:UserProfile
 $Env:<Name>=<Value> - To Create new environment variable
 [environment]::SetEnvironmentvariable("Path", $newValue, "User") to change environment
variable perminantly
Here Strings:
 Here string always begins with @’ and ends with ‘@
 Here string are useful when you required to provide single quotes and double quotes in
single string
 Ex: $s= @'
"Hello world"
"One more line"
‘Get-Process’
Dir
'@

Apple Software Solutions Page 2


Escape Sequence:
Escape sequence is used to print newlines, tab space, alarm sound, etc..

Escape Sequence Special Characters

`n New line

`r Carriage return

`a Alarm

`b Backspace

`' Single quotation mark

`" Double quotation mark

`0 Null

Sub Expressions:
 Variable contains an expression
 $(Expression) is a variable
 $(2+2) is variable with subexpression
 “Result=(2+2)” displays “Result=(2+2)”
 “Result=$(2+2)” displays “Result=4”
 $File=dir d:\PowerShell, $file.Length
 “The size of file is ($file.Length)” does not give expected size
 “The size of file is $($file.Length)” gives expected size
Get-Member:
 An objects properties and methods are referred as its members
 Get-Member is the discovery cmdlet
– Alias: gm
 Don’t assume that cmdlet output shows all or actual properties
– Some properties are calculated or custom
– PowerShell attempts to be IT Pro friendly
 Pipe any command to Get-Member to learn about objects exiting the pipeline

Apple Software Solutions Page 3


Arrays:
• Array is a collection of objects
• PowerShell arrays supports to store multiple data type objects – objects do not have to be
same type
• PowerShell automatically wraps into an array, when there is more than one value/result
• Array elements can access with index number, Index starts from 0
• $RoleNo=100,102,104,106,108; $Servers=1..100 (“..” Range Operator)
• Polymorphic Array : $Student=100,”Suresh”,23,”Hyderabad”,[datetime]”June 10, 1985”
• Create empty array - $Employee=@(); Array only with 1 element - $NoOfStudents=,1
• $<Variable_Name>.Count – Gives number of elements in the array
• $Employee[0] gives first element of an array, $Employee[-1] gives the last element of an
array
• We can access multiple elements of an array; Ex: $List=Dir,$List[1,4,7,12]
• We can’t add/remove values from the default array.
• We need to create an array of type [syste.collections.arraylist] to do add/remove
operations

Apple Software Solutions Page 4


Hash Table:
 Collection of Key/Value pairs
 Do not require to use index to access array members unlike arrays
 Can directly access members with “Key” name
 Create hash table syntax : $Student=@{Key1=“Value1”;Key2=“Value2”}
 Create new hash table : $list = @{Name = "PC01"; IP="10.10.10.10"; User="Tobias Weltner"}
 $List.User,$List[“IP”],$List[“Name”,”IP”]
 $Listy.Keys – lists all keys present in hash table
 Ex: $test=@{Value1=12;Value2=1,2,3,4,5}
 Insert New keys in existing hash table - $List[“Location”]=“Redmond”
 Modifying value : $List[“Location”]=“Hyderabad”
 Remove key from hash table : $List.Remove(“<Key_Name>”)
 Help about_hash_tables

Ordered Hash:
Hash tables are unordered by default
No guarantee what order data will be displayed, Usually not an issue with small hash tables
PowerShell 3.0 introduced [ordered] attribute
$hash=[ordered]@{ A=123; B="foo"; C=3.14 }
Comparison Operators: Used to compare two values.

Operator Conventional Description Example Result


-eq, -ceq, -ieq = Equals 10 –eq 15 $false
-ne, -cne, -ine <> Not Equal 10 –ne 15 $true
greater than or
-ge, -cge, -ige >= equal to 10 -ge 15 $false
-lt, -clt, -ilt < less than or equal to 10 -le 15 $true
-le, -cle, -ile <= less than or equal to 10 -le 15 $true
-contains,
-ccontains,
-icontains contains 1,2,3 -contains 1 $true
-notcontains,
-cnotcontains,
-inotcontains does not contain 1,2,3 –notcontains 1 $false

Apple Software Solutions Page 5


Arithmetic Operator:
These operators used to perform Arithmetic Operators
 Addition -> +
 Subtraction -> -
 Multiplication -> *
 Division -> /
 Modulus -> %
Logical Operator:
These operators used to compare two Relational operators.
 AND -> -AND
 OR -> -OR
 XOR -> -XOR
 NOT -> -NOT

Apple Software Solutions Page 6

You might also like