{"id":791,"date":"2008-03-23T13:28:31","date_gmt":"2008-03-23T21:28:31","guid":{"rendered":"http:\/\/www.scriptinganswers.com\/essentials\/index.php\/2008\/03\/23\/emit-custom-objects-rather-than-text-from-functions-and-scripts\/"},"modified":"2008-11-04T09:07:00","modified_gmt":"2008-11-04T17:07:00","slug":"emit-custom-objects-rather-than-text-from-functions-and-scripts","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/03\/23\/emit-custom-objects-rather-than-text-from-functions-and-scripts\/","title":{"rendered":"Emit custom objects, rather than text, from functions and scripts"},"content":{"rendered":"<p>Whenever possible, functions (especially) and scripts (to a lesser degree) should emit custom objects, not just text. This ensures that the output of these constructs can be piped to other cmdlets, like Where-Object, Sort-Object, and so forth, further extending PowerShell&#8217;s capabilities.<\/p>\n<p>To create a custom object:<\/p>\n<p style=\"margin-left: 40px; \">$obj = New-Object PSObject<\/p>\n<p>To add properties to the object:<\/p>\n<p style=\"margin-left: 40px; \">$obj | Add-Member NoteProperty &#8220;MyPropertyName&#8221; &#8220;MyValue&#8221;<\/p>\n<p>And repeat as needed to add additional properties. Then output the object to the pipeline:<\/p>\n<p style=\"margin-left: 40px; \">Write $obj<\/p>\n<p>These output objects can then be sorted, grouped, filtered, and so forth, making your code behave more like a &#8220;real&#8221; PowerShell cmdlet.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whenever possible, functions (especially) and scripts (to a lesser degree) should emit custom objects, not just text. This ensures that the output of these constructs can be piped to other cmdlets, like Where-Object, Sort-Object, and so forth, further extending PowerShell&#8217;s capabilities. To create a custom object: $obj = New-Object PSObject To add properties to the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[283,25],"tags":[223,351,356,242,28],"class_list":["post-791","post","type-post","status-publish","format-standard","hentry","category-howto","category-windows-powershell","tag-add-member","tag-custom-objects","tag-function-output","tag-new-object","tag-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=791"}],"version-history":[{"count":1,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/791\/revisions"}],"predecessor-version":[{"id":825,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/791\/revisions\/825"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}