List User OU in csv

asked 2020-09-07T13:27:02.237+00:00
roberto vizzini 61 Reputation points

Good afternoon,
I have to export the list of users contained in an OU to a csv file, the code I wrote is the following:

Get-ADUser -Filter * -SearchBase "OU = XX, OU = YY, DC = mydomain, DC = local" -Properties DisplayName | Select-Object -Property @ {Name = "Name"; Expression = {$ _. Displayname}}, @ {Name = "Account"; Expression = {$ _. Samaccountname}} | Sort-Object Name | Export-Csv -Path c: \ temp \ list.csv

So account name and username are all written in one cell, I would like them to be written in two separate cells, one called "account name" and one "username".

Thank you.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,609 questions
No comments
{count} votes

3 additional answers

Sort by: Most helpful
  1. answered 2020-09-07T14:39:01.43+00:00
    Rich Matheisen 34,791 Reputation points

    Unless I've completely misunderstood your code, that should not happen. You're selecting two different properties (which will create a PSCustomObject) and passing the date into a sort and then exporting the object to a CSV.

    First, add the "-NoTypeInformation" switch to your Export-CSV (to eliminate any clutter in the file). Then rerun your code. If you import the CSV into Excel you should see two columns. If you use PowerShell's Import-CSV and examine each row you should see two properties.

    If you'd like (for testing), add "-First 3" to your Select-Object and remove the Export-CSV. You should see three users in a tabular format with the two properties you named in the Select-Object.

    If that's not what you're seeing, post the first three lines of your exported CSV and the code you ran to produce it so we can see what you're experiencing.

    No comments

  2. answered 2020-09-08T04:11:21.813+00:00
    Ian Xue (Shanghai Wicresoft Co., Ltd.) 18,351 Reputation points Microsoft Employee

    Hi,
    I ran the script and the two properties were seperated in Excel. You could open the csv file with notepad and the two properties should be seperated by comma like "Name","Account"

    You could also add "-Delimiter" to Export-Csv to try another delimiter like -Delimiter "|"

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments

  3. answered 2020-09-09T09:50:47.943+00:00
    roberto vizzini 61 Reputation points

    Thanks everyone for your answers.
    Stoyan actually your solution is much simpler, I'll start thinking less complicated.
    Rich, Ian, I followed your suggestions but the problem still presented itself. I imported the csv to Excel on another pc and the output was the desired one, so the problem was on Excel which restored it started to display the output correctly.
    Thanks again for your suggestions.

    Roberto.

    No comments