Manipulating Data Within Powershell Functions Flashcards

1
Q

Name the types of variables.

A
  1. Loosely typed - when a value is assigned to an undefined type of variable
    $variable
  2. Strongly typed - type is assigned to variable
    Variable is an integer, or string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a variable?

A

A unit of memory in which values are stored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the different variable types?

A
  1. User-created variables
  2. Automatic variables
  3. Preference variables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a user created variables?

A

Created and maintained by the user. Variables created within the PS command line exits only while the PS window is open.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are automatic variables?

A

Store the state of PS. PS created these variables and changes their values as required to maintain their accuracy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are preference variables?

A

Store users preference for PS. These variables are created by PS and populated with default values that may be changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create a typed variable for the number 10

A

[Int]$variable1 = 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Created a typed variable for the date January 1, 2023

A

[DateTime]$variable1 = “January 1, 2023”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are six common variable types?

A

Boolean
DateTime
Powershell object
Script block
String
Array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is casting data values?

A

Converting one object type to another

Not all objects can be cast

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When a value is cast to a particular data type, it is a ……

A

One time change

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When a variable is cast to to a particular data type it stays until…..

A

It is updates

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Cast $variable1 = 1 as an integer using the AS operator

A

$Variable1 -as [int]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the F operator?

A

Used to format the values or string into something else

You can begin the statement with the format you want

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Format ‘$variable1 = 123.4567890’ to display only three decimal places

A

“{0:n3}” -f $variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Format ‘$variable1 = 3106359509’ with dashes to look like a phone number

A

“{0:###-###-####}” -f $variable1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you determine what type of variable ‘number1’ is?

A

$number1.GetType().Name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does the dash replace operator do?
-replace

A

It takes the Input string - replace operator - matching pattern - replacement string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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”

A

The input string is ‘$variable1’

The replace operator is ‘-replace’

The matching string is ‘instructor’

The replace string is ‘teacher’

20
Q

What is the regex to switch around the Jones Tom to Tom Jones?

A

([a-z]+)\s([a-z]+) “,’$2, $1’

21
Q

What is the -Split operator?

A

Splits one or more strings into substrings. White space is the delimiter.

22
Q

What is the order of the -Split operator syntax?

A

Delimiter - number of substrings - conditions the delimiter matches

23
Q

You have variable ‘$quarter1 = “Jan Feb Mar”.

If you split how is the output displayed?

A

Jan
Feb
Mar

24
Q

You have variable ‘$quarter1 = “Jan,Feb,Mar”.

If you split how is the output displayed?

A

Jan
Feb
Mar

25
Q

You have variable ‘$quarter1 = “Jan Feb Mar”.

If you split how is the output displayed?

A
26
Q

What does the .Split() Function work?

A

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

27
Q

Separate based on the delimiter using the Split function:

$variable = “Jan,Feb,Mar,Apr,May,Jun”

A

$variable.Split(‘ , ‘’)

28
Q

What is padding string values?

A

Add padding to the LEFT or RIGHT of a string value according the desired length.

29
Q

How would you pad right to add ‘A’ to the end of $variable = “Demonstration”

A

$variable.PadRight(14, ‘A’)

30
Q

How do you create a variable array with comma separated text values for Jan Feb Mar Apr May and June?

A

$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)

31
Q

What are the different ways you can access the following variable array?

$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)

A

$variable
$variable[0]
$variable[0,1,4]
$variable[0..4]

32
Q

How do you access array items using Pipeline with ForEach-Object Loop?

$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)

A

$variable | ForEach-Object {“The month is: $PSItem”}

33
Q

What is $PSItem?

A

Standard PS variable for the output for an ForEach object loop

34
Q

How do you access array items using the ForEach Method?

$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)

A

$variable.ForEach({“The month is: $PSItem”})

35
Q

What are hash tables?

A

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.

36
Q

How to create a hash table?

A

$variable = @{ }

37
Q

Create a country population hashtable.

China = 10
India = 8
US = 6
UK = 2

A

$variable = @{
China = 10;
India = 8;
US = 6;
UK = 2
}

38
Q

How do output the keys in a hashtable?

A

$variable.keys

39
Q

How do you output the values in a hashtable?

A

$variable.values

40
Q

How do you access array items using a ForEach Loop?

$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)

A

ForEach ($item in $variable) {“The month is: $item”}

41
Q

How do you access array items using a For Loop?

$variable = @(‘Jan’,’Feb’,’Mar’,’Apr’,’May’,’Jun’)

A

For ($item = 0; $item -lt $variable.count; $item++). {
“The month is: {0}” -f $variable[$item];
Write-Host “Current Position: $item”
}

42
Q

What are PSCustomObject’s?

A

A simple way to create structured data comprised of both properties and values.

43
Q

How do you create an empty PSCustomObject?

A

$variable = [PSCustomObject]@{ }

$variable = New-Object -TypeName PSObject

44
Q

Common commands for loading and iterating XML and JSON data?

A

ConvertTo-XML

ConvertFrom-Json

ConvertTo-Json

45
Q

What is the simplest approach to use when loading XML?

A

The SELECT-XML command