11---
22description : PSCustomObject is a simple way to create structured data.
33ms.custom : contributor-KevinMarquette
4- ms.date : 10/11/2023
4+ ms.date : 05/06/2024
55title : Everything you wanted to know about PSCustomObject
66---
77# Everything you wanted to know about PSCustomObject
@@ -12,9 +12,9 @@ have a simple way to create structured data. Take a look at the first example an
1212better idea of what that means.
1313
1414> [ !NOTE]
15- > The [ original version] [ original version ] of this article appeared on the blog written by
16- > [ @KevinMarquette ] [ @KevinMarquette ] . The PowerShell team thanks Kevin for sharing this content with
17- > us. Please check out his blog at [ PowerShellExplained.com] [ PowerShellExplained.com ] .
15+ > The [ original version] [ 10 ] of this article appeared on the blog written by
16+ > [ @KevinMarquette ] [ 13 ] . The PowerShell team thanks Kevin for sharing this content with
17+ > us. Please check out his blog at [ PowerShellExplained.com] [ 09 ] .
1818
1919## Creating a PSCustomObject
2020
@@ -60,7 +60,7 @@ properties. One important note is that while this method works, it isn't an exac
6060biggest difference is that the order of the properties isn't preserved.
6161
6262If you want to preserve the order, see
63- [ Ordered hashtables] ( everything-about-hashtable.md#ordered-hashtables ) .
63+ [ Ordered hashtables] [ 05 ] .
6464
6565### Legacy approach
6666
@@ -89,7 +89,7 @@ $myObject = Get-Content -Path $Path | ConvertFrom-Json
8989```
9090
9191I cover more ways to save objects to a file in my article on
92- [ The many ways to read and write to files] [ The many ways to read and write to files ] .
92+ [ The many ways to read and write to files] [ 11 ] .
9393
9494## Working with properties
9595
@@ -113,7 +113,7 @@ $myObject.psobject.properties.remove('ID')
113113
114114The ` .psobject ` is an intrinsic member that gives you access to base object metadata. For more
115115information about intrinsic members, see
116- [ about_Intrinsic_Members] ( /powershell/module/microsoft.powershell.core/about/about_intrinsic_members ) .
116+ [ about_Intrinsic_Members] [ 03 ] .
117117
118118### Enumerating property names
119119
@@ -271,9 +271,8 @@ $myObject.PSObject.TypeNames.Insert(0,"My.Object")
271271```
272272
273273I recently discovered another way to do this from this
274- [ post by /u/markekraus] [ post by /u/markekraus ] . I did a little digging and more posts about the idea
275- from [ Adam Bertram] [ Adam Bertram ] and [ Mike Shepard] [ Mike Shepard ] where they talk about this
276- approach that allows you to define it inline.
274+ [ post by /u/markekraus] [ 14 ] . He talks about this approach that allows you to
275+ define it inline.
277276
278277``` powershell
279278$myObject = [PSCustomObject]@{
@@ -289,13 +288,13 @@ name, we can do some more things.
289288
290289> [ !NOTE]
291290> You can also create custom PowerShell types using PowerShell classes. For more information, see
292- > [ PowerShell Class Overview] ( /powershell/module/Microsoft.PowerShell.Core/About/about_Classes ) .
291+ > [ PowerShell Class Overview] [ 01 ] .
293292
294293## Using DefaultPropertySet (the long way)
295294
296295PowerShell decides for us what properties to display by default. A lot of the native commands have a
297- ` .ps1xml ` [ formatting file] [ formatting file ] that does all the heavy lifting. From this
298- [ post by Boe Prox] [ post by Boe Prox ] , there's another way for us to do this on our custom object
296+ ` .ps1xml ` [ formatting file] [ 08 ] that does all the heavy lifting. From this
297+ [ post by Boe Prox] [ 07 ] , there's another way for us to do this on our custom object
299298using just PowerShell. We can give it a ` MemberSet ` for it to use.
300299
301300``` powershell
@@ -309,7 +308,7 @@ Now when my object just falls to the shell, it will only show those properties b
309308
310309### Update-TypeData with DefaultPropertySet
311310
312- This is nice but I recently saw a better way using [ Update-TypeData] [ Update-TypeData ] to specify
311+ This is nice but I recently saw a better way using [ Update-TypeData] [ 04 ] to specify
313312the default properties.
314313
315314``` powershell
@@ -382,7 +381,7 @@ code or compared to the actual function output.
382381The main reason you would use an output type is so that meta information about your function
383382reflects your intentions. Things like ` Get-Command ` and ` Get-Help ` that your development environment
384383can take advantage of. If you want more information, then take a look at the help for it:
385- [ about_Functions_OutputTypeAttribute] [ about_Functions_OutputTypeAttribute ] .
384+ [ about_Functions_OutputTypeAttribute] [ 02 ] .
386385
387386With that said, if you're using Pester to unit test your functions then it would be a good idea
388387to validate the output objects match your ** OutputType** . This could catch variables that just fall
@@ -400,14 +399,15 @@ the bigger picture and be aware of them when you have an opportunity to use them
400399something and can find a way to work this into your scripts.
401400
402401<!-- link references -->
403- [ original version ] : https://powershellexplained.com/2016-10-28-powershell-everything-you-wanted-to-know-about-pscustomobject/
404- [ powershellexplained.com ] : https://powershellexplained.com/
405- [ @KevinMarquette ] : https://twitter.com/KevinMarquette
406- [ post by Boe Prox ] : https://learn-PowerShell.net/2013/08/03/quick-hits-set-the-default-property-display-in-PowerShell-on-custom-objects/
407- [ formatting file ] : https://mcpmag.com/articles/2014/05/13/PowerShell-properties-part-3.aspx
408- [ about_Functions_OutputTypeAttribute ] : /powershell/module/microsoft.powershell.core/about/about_functions_outputtypeattribute
409- [ The many ways to read and write to files ] : https://powershellexplained.com/2017-03-18-Powershell-reading-and-saving-data-to-files
410- [ post by /u/markekraus ] : https://www.reddit.com/r/PowerShell/comments/590awc/is_it_possible_to_initialize_a_pscustoobject_with/
411- [ Adam Bertram ] : http://www.adamtheautomator.com/
412- [ Mike Shepard ] : https://powershellstation.com/2016/05/22/custom-objects-and-pstypename/
413- [ Update-TypeData ] : /powershell/module/microsoft.powershell.utility/update-typedata
402+ [ 01 ] : /powershell/module/Microsoft.PowerShell.Core/About/about_Classes
403+ [ 02 ] : /powershell/module/microsoft.powershell.core/about/about_functions_outputtypeattribute
404+ [ 03 ] : /powershell/module/microsoft.powershell.core/about/about_intrinsic_members
405+ [ 04 ] : /powershell/module/microsoft.powershell.utility/update-typedata
406+ [ 05 ] : everything-about-hashtable.md#ordered-hashtables
407+ [ 07 ] : https://learn-PowerShell.net/2013/08/03/quick-hits-set-the-default-property-display-in-PowerShell-on-custom-objects/
408+ [ 08 ] : https://mcpmag.com/articles/2014/05/13/PowerShell-properties-part-3.aspx
409+ [ 09 ] : https://powershellexplained.com/
410+ [ 10 ] : https://powershellexplained.com/2016-10-28-powershell-everything-you-wanted-to-know-about-pscustomobject/
411+ [ 11 ] : https://powershellexplained.com/2017-03-18-Powershell-reading-and-saving-data-to-files
412+ [ 13 ] : https://twitter.com/KevinMarquette
413+ [ 14 ] : https://www.reddit.com/r/PowerShell/comments/590awc/is_it_possible_to_initialize_a_pscustoobject_with/
0 commit comments