{"id":179,"date":"2014-12-19T00:01:00","date_gmt":"2014-12-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/12\/19\/access-objects-inside-other-objects-in-powershell-pipeline\/"},"modified":"2019-02-18T10:36:29","modified_gmt":"2019-02-18T17:36:29","slug":"access-objects-inside-other-objects-in-powershell-pipeline","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/access-objects-inside-other-objects-in-powershell-pipeline\/","title":{"rendered":"Access Objects Inside Other Objects in PowerShell Pipeline"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about accessing objects inside other objects in the Windows PowerShell pipeline.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! Yesterday in <a href=\"\/b\/heyscriptingguy\/archive\/2014\/12\/18\/create-custom-powershell-objects.aspx\" target=\"_blank\">Create Custom PowerShell Objects<\/a>, you talked about writing a script to access embedded objects. That seems like a lot of trouble. Is there a better or easier way to do that, for example, when working in the Windows PowerShell console?<\/p>\n<p>&mdash;BZ<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello BZ,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is nearly here! The fifth annual Scripting Guys holiday special! Windows PowerShell MVP, Sean Kearney, has been hard at work on this series for a while. It kicks off tomorrow, December 20, 2014. The series is about Oliver Script, who is a poor IT pro, and who must overcome a series of challenges. The accompanying video, <a href=\"https:\/\/www.youtube.com\/watch?v=iEXZuoI8eCA\" target=\"_blank\">Code Glorious Code<\/a>, is available now, and it should whet your appetite. The series is lots of fun, and if you aren&rsquo;t careful, you might just learn something.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> This is the third in a series of posts about working with objects inside other objects.<\/p>\n<ul>\n<li style=\"list-style-type:none\">\n<ul>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2014\/12\/17\/understand-embedded-objects-in-powershell.aspx\" target=\"_blank\">Understand Embedded Objects in PowerShell<\/a>&nbsp;explains how to use <b>Select-Object<\/b> and <b>&ndash;ExpandProperty<\/b> to look inside an object.<\/li>\n<li><a href=\"\/b\/heyscriptingguy\/archive\/2014\/12\/18\/create-custom-powershell-objects.aspx\" target=\"_blank\">Create Custom PowerShell Objects<\/a> talks about using a script and creating a custom object.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Today I will talk about working inside the pipeline.<\/p>\n<p><span style=\"font-size:12px\">When I try to select a property that contains another object, all I get is the name of the object. This appears when I run a command such as the following:<\/span><\/p>\n<p style=\"margin-left:30px\">Get-Process | Select-Object name, id, startinfo<\/p>\n<p>This is because the <b>StartInfo<\/b><i> <\/i>property contains another object. The output is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-19-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-19-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can use grouping and look inside the <b>StartInfo<\/b><i> <\/i>property:<\/p>\n<p style=\"margin-left:30px\">(Get-Process).startinfo<\/p>\n<p>The output is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-19-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-19-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>So I can now see what is inside the <b>StartInfo<\/b><i> <\/i>property, but I have no way to relate it back to process names or process IDs. Therefore, the information is basically useless. Well&#8230;I will be honest. It is useless except for one thing&mdash;I now know what properties are actually available within <b>StartInfo<\/b>.<\/p>\n<p>Suppose that I am interested in the question, &quot;Does the process load a user profile?&quot; The System.Diagnostics.ProcessStartInfo object has a property called <b>LoadUserProfile<\/b><i>. <\/i>Hmmm, this sounds like exactly what I want to know.<\/p>\n<p>I want the <b>Name<\/b> and <b>ID<\/b> properties from the System.Diagnostics.Process property that returns from the <b>Get-Process<\/b> cmdlet. I also want the <b>LoadUserProfile<\/b><i> <\/i>property from the System.Diagnostics.ProcessStartInfo object contained in the <b>StartInfo<\/b><i> <\/i>property that is returned by <b>Get-Process<\/b>.<\/p>\n<p>This is the classic object contained inside another object dilemma. Because I happen to be playing around at the Windows PowerShell console, I am going to use one of my favorite <b>Select-Object<\/b> tricks. &quot;Wait!&quot; you may say, &quot;The other day when you used <b>Select-Object<\/b>, it would only let you use <b>&ndash;ExpandProperty<\/b> to expand a single property. It did not let you select other properties.&quot;<\/p>\n<p>&#8230;and you would be right.<\/p>\n<p>But what I can do is create a custom object by using a hash table for one of my properties. It is one of my favorite techniques. So I get started. The first part is exactly the same:<\/p>\n<p style=\"margin-left:30px\">Get-Process | Select-Object name, id,<\/p>\n<p>Now I want to create a custom property. So I use a hash table:<\/p>\n<p style=\"margin-left:30px\">Get-Process | Select-Object name, id, @{}<\/p>\n<p>Inside my hash table, I use two well-known properties: <b>Label<\/b> and <b>Expression<\/b>. I can even abbreviate them as <b>L<\/b> and <b>E<\/b>. As shown here, <b>Label<\/b> becomes my custom property name:<\/p>\n<p style=\"margin-left:30px\">Get-Process | Select-Object name, id, @{L=&#039;LoadProfile&#039;;}<\/p>\n<p>For my expression, I use a script block. This is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-Process | Select-Object name, id, @{L=&#039;LoadProfile&#039;;E={}}<\/p>\n<p>Inside the script block, I use <b>$_ <\/b>to represent each process as it comes across the pipeline. I then choose the <b>StartInfo<\/b><i> <\/i>property from the process object, and I select the <b>LoadUserProfile<\/b><i> <\/i>property from the object contained in the <b>StartInfo<\/b><i> <\/i>property. The complete command is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-Process | Select-Object name, id, @{L=&#039;LoadProfile&#039;;E={$_.StartInfo.loadUserProfile}}<\/p>\n<p>When I run the command, the following output appears:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-19-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-19-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>When I pipe the object to <b>Get-Member<\/b>, I see that it is a selected process object. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-19-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-19-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>BZ, that is all there is to using Windows PowerShell objects. Join me tomorrow when the fifth annual Scripting Guys holiday special begins. &nbsp;<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about accessing objects inside other objects in the Windows PowerShell pipeline. &nbsp;Hey, Scripting Guy! Yesterday in Create Custom PowerShell Objects, you talked about writing a script to access embedded objects. That seems like a lot of trouble. Is there a better or easier way to do that, for [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,45],"class_list":["post-179","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about accessing objects inside other objects in the Windows PowerShell pipeline. &nbsp;Hey, Scripting Guy! Yesterday in Create Custom PowerShell Objects, you talked about writing a script to access embedded objects. That seems like a lot of trouble. Is there a better or easier way to do that, for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=179"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}