{"id":9891,"date":"2006-07-31T18:03:00","date_gmt":"2006-07-31T18:03:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/07\/31\/why-do-i-sometimes-get-different-types\/"},"modified":"2019-02-18T13:21:28","modified_gmt":"2019-02-18T20:21:28","slug":"why-do-i-sometimes-get-different-types","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/why-do-i-sometimes-get-different-types\/","title":{"rendered":"Why do I sometimes get different types?"},"content":{"rendered":"<p>PSMDTAG:FAQ: Why do I sometimes get different types or different behaviors?<\/p>\n<p>PSMDTAG:FAQ: When I pipe a heterogenous set of objects to an cmdlet that expects a homogenous stream &#8211; what happens and why?<\/p>\n<p>In our newgroup ( Microsoft.public.Windows.PowerShell ), Alex A asked these questions with the following example:<\/p>\n<blockquote>\n<p dir=\"ltr\"><font face=\"Courier New\" color=\"#0000ff\" size=\"1\"><strong>cd c:\\temp<br \/>$( gci ; gci hkcu:\\ ) | Export-Csv -Path c:\\temp\\f1.csv<br \/>$( gci hkcu:\\ ; gci ) | Export-Csv -Path c:\\temp\\f2.csv <\/strong><\/font><\/p>\n<p dir=\"ltr\">Compare the output of the following 2 commands. In the first case I get all objects being type System.IO.DirectoryInfo; in the second, the only type header is Microsoft.Win32.RegistryKey.<\/p>\n<\/blockquote>\n<p dir=\"ltr\">In this example, the first element of the pipeline generates a heterogenous set of objects and then pipes them to Export-CSV.&nbsp; Export-CSV&nbsp;acts upon a&nbsp;homogeneous stream of objects.&nbsp; Here is literally what Export-CSV does:<\/p>\n<ol>\n<li>\n<div>Get the first object in the stream.&nbsp; <\/div>\n<\/li>\n<li>\n<div>Use its type to look up user-defined serialization directives.<\/div>\n<\/li>\n<li>\n<div>If found, use them, if not, use a default algorithm. <\/div>\n<\/li>\n<ol>\n<li>\n<div>In either case, we now have a&nbsp;well defined set of properties to serialize<\/div>\n<\/li>\n<\/ol>\n<li>\n<div>Open the CSV file and write what type of object we are going to serialize, then write the list of properties that will be serialized<\/div>\n<\/li>\n<li>\n<div>Foreach object in the pipeline:<\/div>\n<\/li>\n<ol>\n<li>\n<div>Write a new line in the file with values for each of the properties.&nbsp; If the value does not exist on that object, write a null<\/div>\n<\/li>\n<\/ol>\n<\/ol>\n<p>Now if you look at the&nbsp;contents of the files, what you will see in the first example is the headers described above, then a set of well-formed&nbsp;lines for the FileInfos in c:\\temp and then a set of sparsely populated lines for the registry keys in hkcu:\\&nbsp; .&nbsp; In the second file, you&#8217;ll find the opposite, a well formed header, a set of well-formed lines for the registy stuff followed by a set of sparsely populated lines for the files in c:\\temp.<\/p>\n<p><strong><font color=\"#ff0000\">In many cases &#8211; the type of the first object in the stream determines the processing for the entire stream.<\/font><\/strong><\/p>\n<p>Now it is important to understand thateach command gets to determine how they are going to handle this case.&nbsp; We&#8217;ve see what Export-CSV does but now see&nbsp;how out-default&nbsp;handles exactly the same situation.&nbsp; out-default is what gets called if you just run a command so just run the following commands:<\/p>\n<blockquote>\n<p><font face=\"Courier New\" color=\"#0000ff\" size=\"1\">cd c:\\temp<br \/>$(gci . ;gci hkcu:\\ )<br \/>$(Gci hkcu:\\ ; gci .)<\/font><\/p>\n<\/blockquote>\n<p>What you should see is that the first expression outputs a regular table for the filesystem objects and then outputs the registry entries as a LIST.&nbsp; The second command outputs the registy objects as a TABLE and outputs the filesystem objects as a LIST.&nbsp; <\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/Aspen Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PSMDTAG:FAQ: Why do I sometimes get different types or different behaviors? PSMDTAG:FAQ: When I pipe a heterogenous set of objects to an cmdlet that expects a homogenous stream &#8211; what happens and why? In our newgroup ( Microsoft.public.Windows.PowerShell ), Alex A asked these questions with the following example: cd c:\\temp$( gci ; gci hkcu:\\ ) [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[10,19,50],"class_list":["post-9891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-faq","tag-internal","tag-types"],"acf":[],"blog_post_summary":"<p>PSMDTAG:FAQ: Why do I sometimes get different types or different behaviors? PSMDTAG:FAQ: When I pipe a heterogenous set of objects to an cmdlet that expects a homogenous stream &#8211; what happens and why? In our newgroup ( Microsoft.public.Windows.PowerShell ), Alex A asked these questions with the following example: cd c:\\temp$( gci ; gci hkcu:\\ ) [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=9891"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9891\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=9891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=9891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=9891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}