How to do it...

Let us now proceed to create a custom object.

  1. To create a custom object, use New-Object cmdlet. 
PS /home/PacktPub> New-Object -TypeName PSCustomObject
  1. That was not of much use to us. Let us add a few properties into it, along with their values and store these values in a variable.
PS> $MyCustomObject = [pscustomobject]@{ 
Name = 'Prashanth Jayaram'
Title = 'PowerShell'
Publisher = 'Packt'
}

PS> $MyCustomObject
  1. To add properties to an object, use the Add-Member cmdlet.
PS> $MyCustomObject | Add-Member -MemberType NoteProperty -Name 'Location' -Value 'United States'
PS> $MyCustomObject
  1. To remove a property from an object, use the following command.
PS> $MyCustomObject.PsObject.Properties.Remove('Location')

PS> # To see if the property is still available, run:
PS> $MyCustomObject.Location
  1. To access properties, you can use the member access operator (.).
PS> $MyCustomObject.Name 
Prashanth Jayaram

PS> $MyCustomObject.Title
PowerShell

PS> $MyCustomObject.Publication
Packt
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.47.221