Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- # 1. Create a PS custom object with three properties
- $person = [PSCustomObject]@{
- Name = "John"
- Age = 30
- Occupation = "Developer"
- }
- # 2. Create, add, insert and manipulate dictionary data structure with this new object in Powershell
- # Create an empty dictionary
- $dictionary = @{}
- # Add the person object to the dictionary with a key of "person"
- $dictionary.Add("person", $person)
- # Retrieve the person object from the dictionary using its key
- $retrievedPerson = $dictionary["person"]
- # Update the age property of the retrieved person object
- $retrievedPerson.Age = 31
- # Insert a new key-value pair into the dictionary using another PS custom object as value
- $newObject = [PSCustomObject]@{
- Property1 = "Value1"
- Property2 = "Value2"
- }
- $dictionary.Add("newObject", $newObject)
- # Print out the updated dictionary
- Write-Output $dictionary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement