{"id":3183,"date":"2013-07-22T00:01:00","date_gmt":"2013-07-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/07\/22\/export-user-names-and-proxy-addresses-to-csv-file\/"},"modified":"2013-07-22T00:01:00","modified_gmt":"2013-07-22T00:01:00","slug":"export-user-names-and-proxy-addresses-to-csv-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/export-user-names-and-proxy-addresses-to-csv-file\/","title":{"rendered":"Export User Names and Proxy Addresses to CSV File"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell to export user names and proxy addresses to a CSV file from Active Directory.<\/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! I am trying to produce a report of our users in Active Directory and their associated proxy addresses. I want it in Excel, so I am using the <strong>Export-CSV<\/strong> cmdlet. The issue is that although I can get the user names just fine, the proxy addresses come back with:<\/p>\n<p style=\"padding-left: 30px\">Microsoft.ActiveDirectory.Management.ADPropertyValueCollection<\/p>\n<p>This does not happen when I print to the screen, only when I export it to a CSV file. Help! This is quite annoying.<\/p>\n<p>&mdash;JF<\/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 JF,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is a new week down here in Charlotte, North Carolina in the United States. The weather has taken a turn for the worse, so instead of getting a daily deluge of rain, we now have hot and humid. Personally, I think I already miss the rain&mdash;I am not a huge fan of hot and humid. Luckily, I do not have to go out too often (one of the great things about working from home), so I can sit in front of a couple of fans, check my email, and get right to work.<\/p>\n<h2>The issue with a multivalued attribute<\/h2>\n<p>The issue with a multivalued attribute, such as the <strong>ProxyAddresses<\/strong> attribute, is that it is an array. This means it contains multiple values that are associated with a single attribute.This makes sense for something like <strong>ProxyAddresses<\/strong> because there could be one or more proxy addresses defined for any particular user in Active Directory Domain Services (AD&nbsp;DS). For something like street address, there is only one value permitted for that attribute because it accepts a single value only.<\/p>\n<p>When I run the following commands, I can easily replicate the issue with the multivalued attribute. (This is a single, logical line command. I broke it at the pipe to display in the blog).<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -Filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Properties proxyaddresses |<\/p>\n<p style=\"padding-left: 30px\">select name, proxyaddresses | Export-CSV -Path c:\\fso\\proxyaddresses.csv &ndash;NoTypeInformation<\/p>\n<p>When I run the command, and open the CSV file in Microsoft Excel, I am greeted with the following output:<span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6457.hsg-7-22-13-01.png\"><img decoding=\"async\" title=\"Image of spreadsheet\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6457.hsg-7-22-13-01.png\" alt=\"Image of spreadsheet\" \/><\/a><\/p>\n<h2>Fixing the issue with multivalued attributes<\/h2>\n<p>Perhaps the easiest way to fix the issue with the multivalued <strong>ProxyAddresses<\/strong> attribute is to create a custom <strong>Select-Object<\/strong> property, then index directly into the array to pull out proxy address 1 and proxy address 2. To do this, I use a hash table to create a new property. The hash table requires two elements: the label and the expression. The label is a string, and the expression is a script block. In Windows PowerShell terms, this means that I can basically do anything I need to do inside the expression element.<\/p>\n<p style=\"padding-left: 30px\">Get-ADUser -Filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Properties proxyaddresses |<\/p>\n<p style=\"padding-left: 30px\">select name, @{L=&#8217;ProxyAddress_1&#8242;; E={$_.proxyaddresses[0]}},<\/p>\n<p style=\"padding-left: 30px\">@{L=&#8217;ProxyAddress_2&#8242;;E={$_.ProxyAddresses[1]}} |<\/p>\n<p style=\"padding-left: 30px\">Export-Csv -Path c:\\fso\\proxyaddresses.csv -NoTypeInformation<\/p>\n<p>I then open the CSV file in Microsoft Excel. The command is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-ADUser -Filter * -SearchBase &#8216;ou=testou,dc=iammred,dc=net&#8217; -Properties pr<\/p>\n<p style=\"padding-left: 30px\">oxyaddresses | select name, @{L=&#8217;ProxyAddress_1&#8242;; E={$_.proxyaddresses[0]}}, @{L=&#8217;Pro<\/p>\n<p style=\"padding-left: 30px\">xyAddress_2&#8242;;E={$_.ProxyAddresses[1]}} | Export-Csv -Path c:\\fso\\proxyaddresses.csv &#8211;<\/p>\n<p style=\"padding-left: 30px\">NoTypeInformation<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0743.hsg-7-22-13-02.png\"><img decoding=\"async\" title=\"Image of command\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0743.hsg-7-22-13-02.png\" alt=\"Image of command\" \/><\/a><\/p>\n<p>The Excel spreadsheet appears, and now I have two columns worth of proxy addresses as shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4034.hsg-7-22-13-03.png\"><img decoding=\"async\" title=\"Image of spreadsheet\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4034.hsg-7-22-13-03.png\" alt=\"Image of spreadsheet\" \/><\/a><\/p>\n<p>JF, that is all there is to using Windows PowerShell to retrieve multivalued attributes and write them to a CSV file. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell to export user names and proxy addresses to a CSV file from Active Directory. &nbsp;Hey, Scripting Guy! I am trying to produce a report of our users in Active Directory and their associated proxy addresses. I want it in Excel, so I am using [&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":[7,169,3,4,20,45],"class_list":["post-3183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-csv-and-other-delimited-files","tag-scripting-guy","tag-scripting-techniques","tag-user-accounts","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell to export user names and proxy addresses to a CSV file from Active Directory. &nbsp;Hey, Scripting Guy! I am trying to produce a report of our users in Active Directory and their associated proxy addresses. I want it in Excel, so I am using [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3183","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=3183"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3183\/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=3183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}