Strings Arrays and Hashtables Flashcards
We create a verbatim string by using…
Double quotes or single quotes.
How can we escape a double quote or single quote when creating a verbatim string?
Double up the double quote or single quote. Eg:
“I just wanted to say ““Hello World””, ok?”
‘I can’‘t believe this’
What is the escape sequence for strings (e.g. new line etc)
Back tick - `
What are the escape sequences for: Backspace New Line Carriage Return CrLf Tab
`b (doesn't work in ISE, only regular script window) `n `r `r`n `t
What are Here Strings?
strings that split out over a number of lines: $heretext = @" Here is some text "@
What are the rules regarding placement of the @” and “@ character sequences with regard to here strings?
@” 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.
When using here strings, is it necessary to use doubled quotes?
No
What is string interpolation?
The ability to dynamically resolve variables within a string. E.g:
$hello = “Hello World”
“This is my greeting: $hello”
How can we directly access a collection of items (e.g. Get-ChildItem) ?
Wrap the command in parentheses. E.g.
(Get-ChildItem).Count
How do we actually display the name of a variable within a string?
Use backtick: E.g.
$items = (Get-ChildItem).Count "`$items = $items"
returns “$items = 123”
Does string interpolation work with single quotes?
NO!
Does string interpolation work with here strings?
Yes :)
How do we embed expressions within a string?
Wrap the expression in $(). E.g
“There are $((Get-ChildItem).Count) items in $(Get-Location)”
How do we get the current date/time in PS?
Get-Date
How can we format strings using C# - esque format?
Use the static Format method of string. E.g.
What’s the PS shortcut for [string]::Format()?
Use -f . E.g.
“There are {0} items” -f $itemCount
“There are {0} items in {1}” -f $itemCount, $location
Why would you use -f formatting rather than string interpolation?
For formatting, e.g. dates, currencies etc
How do you specify number of decimal places for number formatting?
{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 do we specify the number of right-justified spaces to display a number in?
Use
{0,8:N0} E.g.
“{0,8:N0}” -f 123.34 # Will right align the output with at least 8 characters.
How do we format a currency in PS?
{0:C0}
{0:C1}
etc. The output uses the currency symbol for the region that we’re in.
How do we format percentages in PS?
{0:P0} {0:P1} etc. e.g. "{0:P0}" -f 0.1234 returns 12% Note we pass in the fractional part.
How do we format hex values in PS?
{0:X0}
e.g.
“{0:X0}” -f 1234
returns 4D2
How do we front-pad integers?
{0:D8} -f 123
returns
00000123
Note that “D”ecimal only supports integers.
How do we create custom formats in PS?
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
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
How do we perform regex in PS?
use -match then the regex string.
How do we define an array in PS?
use comma separated values:
$array = “Blah”, “di”, “Blah”
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.
How do we change all the contents of an array?
Just use the initialiser syntax:
$array = 1, 2, 3
$array = “Blah”, “di”
What is the formal array creation syntax?
Wrap values in @()
$array = @(1, 2, 3)
How do you create an empty array?
Use
$array = @()
How can we append an entry to an existing array
Use += syntax:
E.g. $array += 3
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
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
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
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 “, “
Hashtables are…
Similar to an array except they use string keys rather than numeric keys
How do you declare a hashtable?
Use $v = @{“Key1” = “Value1”; “Key2” = “Value2”}
How do you access the value associated with a particular key in a hashtable?
Use either bracket or dot notation:
$h[“KeyName”]
$h.”KeyName”
You can use variables as keys in hashtables. How?
$k = "PowerShell" $hash = @{$k = "Blah"} $hash.$k $hash.$($k) $hash.$("Power" + "Shell")
How do you add a new key / value to a hashtable?
$h[“Blah”] = “DiBlah”
How do you remove a key / value from a hashtable?
$h.Remove(“Blah”)
How do you check if a hashtable contains a particular key?
$h.Contains(“Blah”)
or $h.Keys - contains “Blah”
How do you check if a hashtable contains a particular value?
$h.ContainsValue(“Blah”)
or
$h.Values -contains “Blah”
How do I get a list of all keys or all values from a hashtable?
$h.Keys
or
$h.Values