1.Create empty dictionary
2.Add key,value item
3.Access dictionary
4. Measure-Object
4.Iterating dictionary
5.Nested dictionary
6. Remove keys & Clear dictionary
$contacts = @{}
2.Add key,value item
## Way 1:
$key = 1
$value = 'CRM'
$contacts.add( $key, $value )
## Way 2:
$contacts.add( 2, 'C#' )
## Way 3:
$contacts[3]='.NET'
## Way 4 - Keys are just strings
$contacts = @{
'full name' = 'C# Code'
'#' = 1234
}
$contacts['full name'] # OR $contacts.'full name' OR $key = 'full name' $contacts.$key
<#
C# Code
#>
## Creating dictionary with values
$contacts = @{
1 = 'CRM'
2 = 'C#'
31 = '.NET'
}
$contacts[4]='Azure'
$contacts[5]='Cloud'
## print contact dictionary
$contacts
<#
Name Value
---- -----
5 Cloud
4 Azure
31 .NET
2 C#
1 CRM
#>
$contacts_age = @{
1 = 10
2 = 20
3 = 25
}
$key = 1
$value = 'CRM'
$contacts.add( $key, $value )
## Way 2:
$contacts.add( 2, 'C#' )
## Way 3:
$contacts[3]='.NET'
## Way 4 - Keys are just strings
$contacts = @{
'full name' = 'C# Code'
'#' = 1234
}
$contacts['full name'] # OR $contacts.'full name' OR $key = 'full name' $contacts.$key
<#
C# Code
#>
## Creating dictionary with values
$contacts = @{
1 = 'CRM'
2 = 'C#'
31 = '.NET'
}
$contacts[4]='Azure'
$contacts[5]='Cloud'
## print contact dictionary
$contacts
<#
Name Value
---- -----
5 Cloud
4 Azure
31 .NET
2 C#
1 CRM
#>
$contacts_age = @{
1 = 10
2 = 20
3 = 25
}
3.Access dictionary
## By key
$contacts[31]
<#
.NET
#>
## By multi keys
$contacts[2,4] #OR $contacts[(2,4)] OR $contacts[@(2,4)]
<#
Azure
C#
#>
$contacts[31]
<#
.NET
#>
## By multi keys
$contacts[2,4] #OR $contacts[(2,4)] OR $contacts[@(2,4)]
<#
Azure
C#
#>
4. Measure-Object
## Calculates the numeric properties of objects, and the characters, words, and lines in string objects, such as files of text.
## https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/measure-object?view=powershell-5.1
$contacts | Measure-Object
<#
Count : 1
Average :
Sum :
Maximum :
Minimum :
Property :
#>
## number of values
$contacts.count
<#
5
#>
$contacts_age.values | Measure-Object -Average
<#
Count : 3
Average : 18.3333333333333
Sum :
Maximum :
Minimum :
Property :
#>
$contacts_age.values | Measure-Object -Average -Sum -Maximum -Minimum
<#
Count : 3
Average : 18.3333333333333
Sum : 55
Maximum : 25
Minimum : 10
Property :
#>
## https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/measure-object?view=powershell-5.1
$contacts | Measure-Object
<#
Count : 1
Average :
Sum :
Maximum :
Minimum :
Property :
#>
## number of values
$contacts.count
<#
5
#>
$contacts_age.values | Measure-Object -Average
<#
Count : 3
Average : 18.3333333333333
Sum :
Maximum :
Minimum :
Property :
#>
$contacts_age.values | Measure-Object -Average -Sum -Maximum -Minimum
<#
Count : 3
Average : 18.3333333333333
Sum : 55
Maximum : 25
Minimum : 10
Property :
#>
4.Iterating dictionary
## enumerate the keys, to access the values.
$contacts.keys | ForEach-Object{
$contact = 'Contact {0} - {1}' -f $_, $contacts[$_]
Write-Output $contact
}
## OR by foreach loop
foreach($key in $contacts.keys) {
$contact = 'Contact {0} - {1}' -f $key, $contacts[$key]
#Write-Output $contact
}
##using by GetEnumerator()
$contacts.GetEnumerator() | ForEach-Object{
$contact = 'Contact {0} - {1}' -f $_.key, $_.value
#Write-Output $contact
}
<#
Contact 5 - Cloud
Contact 4 - Azure
Contact 31 - .NET
Contact 2 - C#
Contact 1 - CRM
#>
BadEnumeration, System.InvalidOperationException Error - Cannot modify dictionary while it is being enumerated
$contacts.Keys | ForEach-Object {
$contacts[$_] = $contacts[$_] + ' 1'
}
<#
An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute..
At dictionary_example.ps1:122 char:1
+ $contacts.Keys | ForEach-Object {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Collecti...tableEnumerator:HashtableEnumerator) [], RuntimeException
+ FullyQualifiedErrorId : BadEnumeration
#>
## updated last added record
<#
Contact 31 - .NET
Contact 5 - Cloud 1
Contact 4 - Azure
Contact 2 - C#
Contact 1 - CRM
#>
foreach($key in $contacts.keys) {
$contacts[$key] = $contacts[$key] +' 2'
}
<#
Collection was modified; enumeration operation may not execute.
At dictionary_example.ps1:128 char:9
+ foreach($key in $contacts.keys) {
+ ~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException
#>
## updated last first record
<#
Contact 31 - .NET
Contact 5 - Cloud 1 2
Contact 4 - Azure
Contact 2 - C#
Contact 1 - CRM
#>
##Solution to modify, make it clone the keys
$contacts_clone= $contacts.Clone()
$contacts_clone.Keys | ForEach-Object {
$contacts[$_] = $contacts[$_] + ' 3'
}
<#
Contact 31 - .NET 3
Contact 5 - Cloud 1 2 3
Contact 4 - Azure 3
Contact 2 - C# 3
Contact 1 - CRM 3
#>
$contacts.keys | ForEach-Object{
$contact = 'Contact {0} - {1}' -f $_, $contacts[$_]
Write-Output $contact
}
## OR by foreach loop
foreach($key in $contacts.keys) {
$contact = 'Contact {0} - {1}' -f $key, $contacts[$key]
#Write-Output $contact
}
##using by GetEnumerator()
$contacts.GetEnumerator() | ForEach-Object{
$contact = 'Contact {0} - {1}' -f $_.key, $_.value
#Write-Output $contact
}
<#
Contact 5 - Cloud
Contact 4 - Azure
Contact 31 - .NET
Contact 2 - C#
Contact 1 - CRM
#>
BadEnumeration, System.InvalidOperationException Error - Cannot modify dictionary while it is being enumerated
$contacts.Keys | ForEach-Object {
$contacts[$_] = $contacts[$_] + ' 1'
}
<#
An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute..
At dictionary_example.ps1:122 char:1
+ $contacts.Keys | ForEach-Object {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Collecti...tableEnumerator:HashtableEnumerator) [], RuntimeException
+ FullyQualifiedErrorId : BadEnumeration
#>
## updated last added record
<#
Contact 31 - .NET
Contact 5 - Cloud 1
Contact 4 - Azure
Contact 2 - C#
Contact 1 - CRM
#>
foreach($key in $contacts.keys) {
$contacts[$key] = $contacts[$key] +' 2'
}
<#
Collection was modified; enumeration operation may not execute.
At dictionary_example.ps1:128 char:9
+ foreach($key in $contacts.keys) {
+ ~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException
#>
## updated last first record
<#
Contact 31 - .NET
Contact 5 - Cloud 1 2
Contact 4 - Azure
Contact 2 - C#
Contact 1 - CRM
#>
##Solution to modify, make it clone the keys
$contacts_clone= $contacts.Clone()
$contacts_clone.Keys | ForEach-Object {
$contacts[$_] = $contacts[$_] + ' 3'
}
<#
Contact 31 - .NET 3
Contact 5 - Cloud 1 2 3
Contact 4 - Azure 3
Contact 2 - C# 3
Contact 1 - CRM 3
#>
5.Nested dictionary
$contacts = @{
name = 'PoweShell'
age = 10
}
$contacts.location = @{}
$contacts.location.city = 'Middleton'
$contacts.location.state = 'WI'
# OR
$contacts = @{
name = 'PoweShell'
age = 10
homelocation = @{
city = 'Middleton'
state = 'WI'
}
officelocation = @{
city = 'Middleton'
state = 'WI'
}
}
$contacts
<#
homelocation {city, state}
name PoweShell
age 10
officelocation {city, state}
#>
$contacts.homelocation
<#
city Middleton
state WI
#>
$contacts.homelocation.city
## OR $contacts.homelocation['city'] OR $contacts['homelocation'].city OR $contacts['homelocation']['city']
<#
Middleton
#>
$contacts = @{
CSharp = @{
company = 'MicroSoft'
year = 2010
}
Java = @{
company = 'Sun'
year = 2000
}
}
## display nested values
foreach($name in $contacts.keys)
{
$contact = $contacts[$name]
$cv = '{0},{1},{2}' -f $name, $contact.company, $contact.year
Write-Output $cv
}
<#
CSharp,MicroSoft,2010
Java,Sun,2000
#>
## Convert to JSON
$contacts | ConvertTo-JSON
<#
{
"CSharp": {
"year": 2010,
"company": "MicroSoft"
},
"Java": {
"year": 2000,
"company": "Sun"
}
}
#>
name = 'PoweShell'
age = 10
}
$contacts.location = @{}
$contacts.location.city = 'Middleton'
$contacts.location.state = 'WI'
# OR
$contacts = @{
name = 'PoweShell'
age = 10
homelocation = @{
city = 'Middleton'
state = 'WI'
}
officelocation = @{
city = 'Middleton'
state = 'WI'
}
}
$contacts
<#
homelocation {city, state}
name PoweShell
age 10
officelocation {city, state}
#>
$contacts.homelocation
<#
city Middleton
state WI
#>
$contacts.homelocation.city
## OR $contacts.homelocation['city'] OR $contacts['homelocation'].city OR $contacts['homelocation']['city']
<#
Middleton
#>
$contacts = @{
CSharp = @{
company = 'MicroSoft'
year = 2010
}
Java = @{
company = 'Sun'
year = 2000
}
}
## display nested values
foreach($name in $contacts.keys)
{
$contact = $contacts[$name]
$cv = '{0},{1},{2}' -f $name, $contact.company, $contact.year
Write-Output $cv
}
<#
CSharp,MicroSoft,2010
Java,Sun,2000
#>
## Convert to JSON
$contacts | ConvertTo-JSON
<#
{
"CSharp": {
"year": 2010,
"company": "MicroSoft"
},
"Java": {
"year": 2000,
"company": "Sun"
}
}
#>
6. Remove keys & Clear dictionary
## remove keys
$contacts.remove(3)
<#
Contact 5 - Cloud 1 2 3
Contact 4 - Azure 3
Contact 2 - C# 3
Contact 1 - CRM 3
#>
##empty the dictionary
$contacts.Clear()
##OR initalize
$contacts = @{}
$contacts.remove(3)
<#
Contact 5 - Cloud 1 2 3
Contact 4 - Azure 3
Contact 2 - C# 3
Contact 1 - CRM 3
#>
##empty the dictionary
$contacts.Clear()
##OR initalize
$contacts = @{}
No comments:
Post a Comment