Strings Arrays and Hashtables Flashcards

1
Q

We create a verbatim string by using…

A

Double quotes or single quotes.

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

How can we escape a double quote or single quote when creating a verbatim string?

A

Double up the double quote or single quote. Eg:
“I just wanted to say ““Hello World””, ok?”
‘I can’‘t believe this’

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

What is the escape sequence for strings (e.g. new line etc)

A

Back tick - `

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
What are the escape sequences for:
Backspace
New Line
Carriage Return
CrLf
Tab
A
`b (doesn't work in ISE, only regular script window)
`n
`r
`r`n
`t
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are Here Strings?

A
strings that split out over a number of lines:
$heretext = @"
Here 
is 
some
text
"@
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the rules regarding placement of the @” and “@ character sequences with regard to here strings?

A

@” has to be that the last character sequence at the end of the line (typically just after a variable declaration). The “@ characters have to be at the beginning of a new line.

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

When using here strings, is it necessary to use doubled quotes?

A

No

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

What is string interpolation?

A

The ability to dynamically resolve variables within a string. E.g:
$hello = “Hello World”
“This is my greeting: $hello”

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

How can we directly access a collection of items (e.g. Get-ChildItem) ?

A

Wrap the command in parentheses. E.g.

(Get-ChildItem).Count

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

How do we actually display the name of a variable within a string?

A

Use backtick: E.g.

$items = (Get-ChildItem).Count
"`$items = $items"

returns “$items = 123”

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

Does string interpolation work with single quotes?

A

NO!

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

Does string interpolation work with here strings?

A

Yes :)

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

How do we embed expressions within a string?

A

Wrap the expression in $(). E.g

“There are $((Get-ChildItem).Count) items in $(Get-Location)”

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

How do we get the current date/time in PS?

A

Get-Date

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

How can we format strings using C# - esque format?

A

Use the static Format method of string. E.g.

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

What’s the PS shortcut for [string]::Format()?

A

Use -f . E.g.
“There are {0} items” -f $itemCount
“There are {0} items in {1}” -f $itemCount, $location

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

Why would you use -f formatting rather than string interpolation?

A

For formatting, e.g. dates, currencies etc

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

How do you specify number of decimal places for number formatting?

A

{0:N[# of places]}. E.g.
“{0:N0}” -f 123.34 # returns 123
“{0:N1}” -f 123.34 # returns 123.3
“{0:N2}” -f 123.34 # returns 123.34
“{0:N3}” -f 123.34 # returns 123.340

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

How do we specify the number of right-justified spaces to display a number in?

A

Use
{0,8:N0} E.g.
“{0,8:N0}” -f 123.34 # Will right align the output with at least 8 characters.

20
Q

How do we format a currency in PS?

A

{0:C0}
{0:C1}
etc. The output uses the currency symbol for the region that we’re in.

21
Q

How do we format percentages in PS?

A
{0:P0}
{0:P1}
etc.
e.g. "{0:P0}" -f 0.1234
returns 12%
Note we pass in the fractional part.
22
Q

How do we format hex values in PS?

A

{0:X0}
e.g.
“{0:X0}” -f 1234
returns 4D2

23
Q

How do we front-pad integers?

A

{0:D8} -f 123
returns
00000123

Note that “D”ecimal only supports integers.

24
Q

How do we create custom formats in PS?

A
Use pound sign:
"{0:#,#.###}" -f 123
Gives: 123
"{0:#,#.###}" -f 123.1245
Gives 123.125
Use zeroes after the decimal point to force to that number of places:

“{0:#,#.000}” -f 123.1
gives 123.100

25
How can we use wildcards in PS?
Use -like and: * = match zero or more characters ? = match exactly a single character [a-d] The character matches one in the inclusive range
26
How do we perform regex in PS?
use -match then the regex string.
27
How do we define an array in PS?
use comma separated values: | $array = "Blah", "di", "Blah"
28
How do we return all values stored in an array?
just use the array variable. E.g.: ``` $array = "Blah", "di", "Blah" $array # returns all values in the array on separate lines. ```
29
How do we change all the contents of an array?
Just use the initialiser syntax: $array = 1, 2, 3 $array = "Blah", "di"
30
What is the formal array creation syntax?
Wrap values in @() $array = @(1, 2, 3)
31
How do you create an empty array?
Use | $array = @()
32
How can we append an entry to an existing array
Use += syntax: | E.g. $array += 3
33
What is numeric range notation and how can it be used to create and initialise an array?
``` It's a double dot notation. E.g. $array = 1..5 # Loads the array with values 1 through 5 inclusive ```
34
How can we tell whether a value exists (or not) in an array?
``` Use -contains and -notcontains e.g. $array = 1, 42, 56 $array -contains 42 # returns true $array -contains 12 # returns false $array -notcontains 42 # returns false ```
35
How do we create multi-dimensional arrays?
Create individual arrays and then include those individual arrays in a new array: $a = 1, 2, 3 $b = 4, 5, 6 $c = $a, $b
36
How do we join all the elements of an array together?
Use -join with the character that you wish to use as the delimiter. $array -join ", "
37
Hashtables are...
Similar to an array except they use string keys rather than numeric keys
38
How do you declare a hashtable?
Use $v = @{"Key1" = "Value1"; "Key2" = "Value2"}
39
How do you access the value associated with a particular key in a hashtable?
Use either bracket or dot notation: $h["KeyName"] $h."KeyName"
40
You can use variables as keys in hashtables. How?
``` $k = "PowerShell" $hash = @{$k = "Blah"} $hash.$k $hash.$($k) $hash.$("Power" + "Shell") ```
41
How do you add a new key / value to a hashtable?
$h["Blah"] = "DiBlah"
42
How do you remove a key / value from a hashtable?
$h.Remove("Blah")
43
How do you check if a hashtable contains a particular key?
$h.Contains("Blah") | or $h.Keys - contains "Blah"
44
How do you check if a hashtable contains a particular value?
$h.ContainsValue("Blah") or $h.Values -contains "Blah"
45
How do I get a list of all keys or all values from a hashtable?
$h.Keys or $h.Values