-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_Hashtables Lists and Arrays.ps1
153 lines (112 loc) · 4.22 KB
/
4_Hashtables Lists and Arrays.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -------------------------------------------------------
# Collections
# -------------------------------------------------------
# Hashtables, Lists and Arrays are all objects that hold a
# collection of other objects. Think of them as containers,
# throw the object in a container to pull it out later
# -------------------------------------------------------
# Arrays
# -------------------------------------------------------
# Arrays are a simple collection of objects in PowerShell.
# These are simpler than a List and are generally used instead
# of Lists. These objects can be anything
# Creating an Array
$colors = @("Red", "Green", "Blue")
$colors = "Red", "Green", "Blue"
$colors.RemoveAt(0)
# Items are accessed via a 0 index list, this means first item is index 0
$colors[0]
$colors[1]
$colors[2]
$colors[3]
# Adding item to array
$colors += "Yellow"
# Remove item from an array
$colors = $colors | Where-Object {$_ -ne "Yellow"} # <- Technically this recreates the array with all items BUT the "Blue"
# Lists are much more efficient at working with large sets of data, consider using them instead of plain Arrays
# -------------------------------------------------------
# Hashtables
# -------------------------------------------------------
# Hashtables are a collection of Key <-> Value pairs. (Dictionary) PowerShell
# is great in that it lets you create these on the fly and access
# them with either . notation or [] notation.
$hash = @{
Item1 = "First Item"
Item2 = "Second Item"
Item3 = "Third Item"
MyItem = Get-Date
}
# Hashtable items are accessed by the Key or by Property with the
# same name as they Key
$hash.MyItem
$hash["MyItem"]
# Adding an item to a Hashtable
$hash["lkjasdflhkasdf"] = "Fourth Item"
# Removing item from a Hashtable
$hash.Remove(0)
# Hashtables have no order by default, if Order matters you can create
# an ordered Hashtable like so
$hash = [ordered]@{
Item1 = "First Item"
Item2 = "Second Item"
Item3 = "Third Item"
}
# -------------------------------------------------------
# Lists
# -------------------------------------------------------
# Lists are a collection of objects accessed via a list index and
# are much more efficient than Arrays. Lists contain strongly
# typed objects so type of object must be known. Or you can
# take advantage of Polymorphism and use PSObject
$colors = New-Object -TypeName System.Collections.Generic.List[String]
# Add String to List
$colors.Add("Red")
$colors.Add("Green")
$colors.Add("Blue")
# Access string
$colors[0]
# Remove string
$colors.RemoveAt(0)
# In order to store any kind of object
$objects = New-Object -TypeName System.Collections.Generic.List[PSObject]
$objects.Add("Test")
$objects.Add(31)
$objects.Add((Get-Date))
$objects[2].Year
# -------------------------------------------------------
# Sample Array/List Operations
# -------------------------------------------------------
$colors | ForEach-Object {
Write-Output "The current color is: $($_)"
}
$colors.GetType()
$colors.ToUpper()
$colors.ToUpper() | ForEach-Object {
Write-Output "The current color is: $($_)"
}
# -------------------------------------------------------
# Hashtable to PSCustomObject
# -------------------------------------------------------
# The biggest difference is output when formatting to screen
# or file. This type of conversion you will see quite often
# Create hash table
$hash = [ordered]@{
Item1 = "Item_1"
Item2 = "Item_2"
Item3 = "Item_3"
}
$hash["Tim"] = "Davis"
# Create PSCustomObject from Hashtable (Quick Way)
$obj = New-Object -TypeName PSCustomObject -Property $hash
$obj | Get-Member
# Create PSCustomObject (Long Way)
$obj = New-Object -TypeName PSCustomObject
$obj | Add-Member -NotePropertyName Item1 -NotePropertyValue "Item_1"
$obj | Add-Member -NotePropertyName Item2 -NotePropertyValue "Item_2"
$obj | Add-Member -NotePropertyName Item3 -NotePropertyValue "Item_3"
# Display to screen
$hash | Format-Table
$obj | Format-Table
# Export to CSV
$hash | Export-Csv -Path .\hash_csv.csv -NoTypeInformation
$obj | Export-Csv -Path .\obj_csv.csv -NoTypeInformation