Manipulating Data Within Powershell Functions Flashcards
Name the types of variables.
- Loosely typed - when a value is assigned to an undefined type of variable
$variable - Strongly typed - type is assigned to variable
Variable is an integer, or string
What is a variable?
A unit of memory in which values are stored
What are the different variable types?
- User-created variables
- Automatic variables
- Preference variables
What is a user created variables?
Created and maintained by the user. Variables created within the PS command line exits only while the PS window is open.
What are automatic variables?
Store the state of PS. PS created these variables and changes their values as required to maintain their accuracy.
What are preference variables?
Store users preference for PS. These variables are created by PS and populated with default values that may be changed.
Create a typed variable for the number 10
[Int]$variable1 = 10
Created a typed variable for the date January 1, 2023
[DateTime]$variable1 = “January 1, 2023”
What are six common variable types?
Boolean
DateTime
Powershell object
Script block
String
Array
What is casting data values?
Converting one object type to another
Not all objects can be cast
When a value is cast to a particular data type, it is a ……
One time change
When a variable is cast to to a particular data type it stays until…..
It is updates
Cast $variable1 = 1 as an integer using the AS operator
$Variable1 -as [int]
What is the F operator?
Used to format the values or string into something else
You can begin the statement with the format you want
Format ‘$variable1 = 123.4567890’ to display only three decimal places
“{0:n3}” -f $variable
Format ‘$variable1 = 3106359509’ with dashes to look like a phone number
“{0:###-###-####}” -f $variable1
How do you determine what type of variable ‘number1’ is?
$number1.GetType().Name
What does the dash replace operator do?
-replace
It takes the Input string - replace operator - matching pattern - replacement string
What is the input value, replace operator, matching string, and replacement value for the following?
$variable1 = “The class instructor asked for a volunteer for a demonstration”
The input string is ‘$variable1’
The replace operator is ‘-replace’
The matching string is ‘instructor’
The replace string is ‘teacher’
What is the regex to switch around the Jones Tom to Tom Jones?
([a-z]+)\s([a-z]+) “,’$2, $1’
What is the -Split operator?
Splits one or more strings into substrings. White space is the delimiter.
What is the order of the -Split operator syntax?
Delimiter - number of substrings - conditions the delimiter matches
You have variable ‘$quarter1 = “Jan Feb Mar”.
If you split how is the output displayed?
Jan
Feb
Mar
You have variable ‘$quarter1 = “Jan,Feb,Mar”.
If you split how is the output displayed?
Jan
Feb
Mar
You have variable ‘$quarter1 = “Jan Feb Mar”.
If you split how is the output displayed?
What does the .Split() Function work?
Splits input into multiple substrings based on the delimiters
Uses white space characters like space, tabs, and line breaks as default delimiter
Available for all STRING type variables and values
Separate based on the delimiter using the Split function:
$variable = “Jan,Feb,Mar,Apr,May,Jun”
$variable.Split(‘ , ‘’)
What is padding string values?
Add padding to the LEFT or RIGHT of a string value according the desired length.
How would you pad right to add ‘A’ to the end of $variable = “Demonstration”
$variable.PadRight(14, ‘A’)
How do you create a variable array with comma separated text values for Jan Feb Mar Apr May and June?
$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)
What are the different ways you can access the following variable array?
$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)
$variable
$variable[0]
$variable[0,1,4]
$variable[0..4]
How do you access array items using Pipeline with ForEach-Object Loop?
$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)
$variable | ForEach-Object {“The month is: $PSItem”}
What is $PSItem?
Standard PS variable for the output for an ForEach object loop
How do you access array items using the ForEach Method?
$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)
$variable.ForEach({“The month is: $PSItem”})
What are hash tables?
Like an array, except you can store every value (object) using a key. Has tables only contain values, not properties. They are a key value store.
Also known as a dictionary or associative array.
How to create a hash table?
$variable = @{ }
Create a country population hashtable.
China = 10
India = 8
US = 6
UK = 2
$variable = @{
China = 10;
India = 8;
US = 6;
UK = 2
}
How do output the keys in a hashtable?
$variable.keys
How do you output the values in a hashtable?
$variable.values
How do you access array items using a ForEach Loop?
$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)
ForEach ($item in $variable) {“The month is: $item”}
How do you access array items using a For Loop?
$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)
For ($item = 0; $item -lt $variable.count; $item++). {
“The month is: {0}” -f $variable[$item];
Write-Host “Current Position: $item”
}
What are PSCustomObject’s?
A simple way to create structured data comprised of both properties and values.
How do you create an empty PSCustomObject?
$variable = [PSCustomObject]@{ }
$variable = New-Object -TypeName PSObject
Common commands for loading and iterating XML and JSON data?
ConvertTo-XML
ConvertFrom-Json
ConvertTo-Json
What is the simplest approach to use when loading XML?
The SELECT-XML command