In: Computer Science
Initialize an empty hashtable, programmatically add 5 items (colors, names, fruits, etc.), use Write-Host to print the keys and values separately, then remove one of the items and print the entire hashtable
POWERSHELL PLEASE!
Initializing an empty hashtable:
$hash = @{}
adding 5 items:
Syntax: $hash = @{ key1 = value1 ; key2 = value2; key3 = value3 ; key4=value4;key5=value5}
$hash = @{ ID = 1; Color = "Red" ; Name = "Bhanu"; Fruit="Apple" ; Shape = "Square"}
we can also use Ordered dictionaries to maintain the order in which entries are added as shown below:
$hash = [ordered]@{ ID = 1; Color = "Red" ; name = "Bhanu"; fruit="Apple" ; Shape = "Square"}
Print the keys using Write-host:
write-host("Print all hashtable keys") $hash.keys
Output:
Print all hashtable keys ID Color Name Fruit Shape
Print the Values using Write-host:
write-host("Print all hashtable values") $hash.values
Output:
Print all hashtable values 1 Red Bhanu Apple Square
Removing one of the item(Fruit):
write-host("Removing an item")
$hash.Remove("Fruit")
printing the entire hashtable:
$hash
Output:
Name Value ---- ----- ID 1 Color Red Name Bhanu Shape Square