{"id":5451,"date":"2020-05-11T17:00:00","date_gmt":"2020-05-11T15:00:00","guid":{"rendered":"https:\/\/www.razumny.no\/?p=5451"},"modified":"2020-04-25T23:14:18","modified_gmt":"2020-04-25T21:14:18","slug":"powershell-pipe-output-to-csv-file","status":"publish","type":"post","link":"https:\/\/www.razumny.no\/?p=5451","title":{"rendered":"PowerShell: Pipe output to CSV file"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.razumny.no\/?p=5450\">Last week<\/a>, I showed you how to get specific properties for all users in a given OU. Knowing that the output of that query quickly gets hard to navigate, wanting to remove the extraneous data that the customer didn&#8217;t request, and assuming that they wanted to manipulate the data, I decided to return the results as a CSV-file. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Naturally, PowerShell has an applet for this, We simply take the command we bult last week, and <em>pipe<\/em> it into that applet. In computing terms, a pipe denotes this character: <code>|<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The applet is called <code>Export-CSV<\/code>. It requires that you specify the location and name of the output file. As a result, the query looked like this:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Get-ADUser -Filter * -SearchBase \"OU=Finance,OU=UserAccounts,DC=FABRIKAM,DC=COM\" -properties CN, DisplayName, LastLogonDate, Manager | Export-CSV -path .\\OUName.csv<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because my customers operate in Norway, there are the occasional names with the three letters of the Norwegian alphabet &#8211; \u00c6, \u00d8, and \u00c5. Normally, these would be output as &#8220;?&#8221;. To avoid this, I tacked on the <code>Encoding<\/code> parameter to ensure that the output was encoded in Unicode. This resulted in the following query:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Get-ADUser -Filter * -SearchBase \"OU=Finance,OU=UserAccounts,DC=FABRIKAM,DC=COM\" -properties CN, DisplayName, LastLogonDate, Manager | Export-CSV -path .\\OUName.csv -Encoding Unicode<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes, we want to run the same query on multiple users or groups. To achieve this, we use the <code>Append<\/code> parameter, like so:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Get-ADUser -Filter * -SearchBase \"OU=Finance,OU=UserAccounts,DC=FABRIKAM,DC=COM\" -properties CN, DisplayName, LastLogonDate, Manager | Export-CSV -path .\\OUName.csv -Encoding Unicode -Append<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week, I showed you how to get specific properties for all users in a given OU. Knowing that the output of that query quickly gets hard to navigate, wanting to remove the extraneous data that the customer didn&#8217;t request, and assuming that they wanted to manipulate the data, I decided to return the results [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[820],"tags":[1244,969],"class_list":["post-5451","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-export-csv","tag-powershell"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p2ukSU-1pV","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":6303,"url":"https:\/\/www.razumny.no\/?p=6303","url_meta":{"origin":5451,"position":0},"title":"Finding all groups without a manager in a specific OU in AD","author":"Aleksander Nordgarden-R\u00f8dner","date":"01\/03\/2021","format":false,"excerpt":"Last week, I showed you how you can easily find the OU to use when looking for the members of a specific OU. Today, I'd like to show you how I use that information. The background was that we use AD groups to control access to network shares. In order\u2026","rel":"","context":"In &quot;How To&quot;","block_context":{"text":"How To","link":"https:\/\/www.razumny.no\/?cat=615"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.razumny.no\/wp-content\/uploads\/image-47.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":4297,"url":"https:\/\/www.razumny.no\/?p=4297","url_meta":{"origin":5451,"position":1},"title":"Powershell: Exporting Active Directory Contacts","author":"Aleksander Nordgarden-R\u00f8dner","date":"16\/10\/2017","format":false,"excerpt":"Some time ago, I needed to have a list of all Contacts registered in Active Directory. Knowing that there are a lot of them (numbering at least eighty), getting the data manually was not a viable alternative, particularly knowing that the same objective can be achieved through Powershell. I eventually\u2026","rel":"","context":"In &quot;code examples&quot;","block_context":{"text":"code examples","link":"https:\/\/www.razumny.no\/?cat=119"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5478,"url":"https:\/\/www.razumny.no\/?p=5478","url_meta":{"origin":5451,"position":2},"title":"Excel: Text to Columns","author":"Aleksander Nordgarden-R\u00f8dner","date":"25\/05\/2020","format":false,"excerpt":"I work a lot with text files containing data which is, to some degree or another, structured. Whether a breach file from a published breach, or the result of a powershell query such as Get-ADUser, Get-ADComputer, or Get-ADDirectReports, I need to separate the data into columns so that I can\u2026","rel":"","context":"In &quot;How To&quot;","block_context":{"text":"How To","link":"https:\/\/www.razumny.no\/?cat=615"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.razumny.no\/wp-content\/uploads\/image-16.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":5450,"url":"https:\/\/www.razumny.no\/?p=5450","url_meta":{"origin":5451,"position":3},"title":"PowerShell: Getting properties for all user objects in a given OU","author":"Aleksander Nordgarden-R\u00f8dner","date":"04\/05\/2020","format":false,"excerpt":"Two weeks ago, I showed you how I got some specific properties for a specific user. Perhaps unsurprisingly, the next request from my customer was a similar dump from a specific OU. While the Identity parameter works well when you're dealing with a specific user, it doesn't help when you\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/www.razumny.no\/?cat=820"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5446,"url":"https:\/\/www.razumny.no\/?p=5446","url_meta":{"origin":5451,"position":4},"title":"Powershell: Retrieving all properties for a given user object","author":"Aleksander Nordgarden-R\u00f8dner","date":"20\/04\/2020","format":false,"excerpt":"I was asked to provide an overview of all available properties in an Active Directory user object for an upcoming project. As it turns out, that is very easily accomplished. Unsurprisingly, PowerShell has an applet for this. We use the Get-ADUser applet, with the parameters Identity to identify the user\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/www.razumny.no\/?cat=820"},"img":{"alt_text":"Output of get-aduser command, filtering on identity and specific proeprties","src":"https:\/\/i0.wp.com\/www.razumny.no\/wp-content\/uploads\/image-13.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.razumny.no\/wp-content\/uploads\/image-13.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.razumny.no\/wp-content\/uploads\/image-13.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.razumny.no\/wp-content\/uploads\/image-13.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":5459,"url":"https:\/\/www.razumny.no\/?p=5459","url_meta":{"origin":5451,"position":5},"title":"PowerShell: Check computer last logon date","author":"Aleksander Nordgarden-R\u00f8dner","date":"18\/05\/2020","format":false,"excerpt":"In a recently closed ticket, I had specified that the solution would be automatically applied upon reboot of a computer. As so often happens one of the twenty or so users affected by the solution emailed to say that it didn't work. Having dealt with said user before, I had\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/www.razumny.no\/?cat=820"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/posts\/5451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.razumny.no\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5451"}],"version-history":[{"count":4,"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/posts\/5451\/revisions"}],"predecessor-version":[{"id":5470,"href":"https:\/\/www.razumny.no\/index.php?rest_route=\/wp\/v2\/posts\/5451\/revisions\/5470"}],"wp:attachment":[{"href":"https:\/\/www.razumny.no\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.razumny.no\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.razumny.no\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}