Let us now proceed to create a custom object.
- To create a custom object, use New-Object cmdlet.
PS /home/PacktPub> New-Object -TypeName PSCustomObject
- 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
- To add properties to an object, use the Add-Member cmdlet.
PS> $MyCustomObject | Add-Member -MemberType NoteProperty -Name 'Location' -Value 'United States'
PS> $MyCustomObject
- 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
- To access properties, you can use the member access operator (.).
PS> $MyCustomObject.Name
Prashanth Jayaram
PS> $MyCustomObject.Title
PowerShell
PS> $MyCustomObject.Publication
Packt