Advertisement
wetyukmnbxc

Object and dictionary

Mar 27th, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Example code to show how to 1. create a pscustom object with three properties ;  2.  create , add , insert and manipulate dictionary data structure with this new object in Powershell.
  2.  
  3.  
  4. # 1. Create a PS custom object with three properties
  5. $person = [PSCustomObject]@{
  6.     Name = "John"
  7.     Age = 30
  8.     Occupation = "Developer"
  9. }
  10.  
  11. # 2. Create, add, insert and manipulate dictionary data structure with this new object in Powershell
  12.  
  13. # Create an empty dictionary
  14. $dictionary = @{}
  15.  
  16. # Add the person object to the dictionary with a key of "person"
  17. $dictionary.Add("person", $person)
  18.  
  19. # Retrieve the person object from the dictionary using its key
  20. $retrievedPerson = $dictionary["person"]
  21.  
  22. # Update the age property of the retrieved person object
  23. $retrievedPerson.Age = 31
  24.  
  25. # Insert a new key-value pair into the dictionary using another PS custom object as value
  26. $newObject = [PSCustomObject]@{
  27.     Property1 = "Value1"
  28.     Property2 = "Value2"
  29. }
  30. $dictionary.Add("newObject", $newObject)
  31.  
  32. # Print out the updated dictionary
  33. Write-Output $dictionary
  34.  
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement