0

I have this line of code that gets me all the AD users from my Active Directory server but when I look the CSV file it generates (open with Excel) then the file is not formatted properly.

Get-ADUser -Filter * -SearchBase "DC=mydomainname, dc=local" |
    Select-Object name, samaccountname |
    Export-Csv -path "\\mypcname\c$\ADusers.csv" -NoTypeInformation -Encoding UTF8

I expected the output to be like:

Column1 Column2
Jon Doe JDoe  

And it is:

name,"samaccountname"
Guest,"Guest"

all in one column.

Edit:

Requested screenshot:

Excel screenshot

11
  • Normally, we do not approve of screen shots, but this is an exception. Please load the file into Excel, and then take a screen shot of Excel with this file displayed and edit the screen shot into your question here. The text that you've posted is a proper CSV file, and if your Excel is working properly, you should see two columns, headed name and samaccountname, and the respective values, one user per line, below. Commented Aug 29, 2019 at 10:56
  • 2
    Try doing the export without the -Encoding UTF8. Excel is not recognizing your file as a proper CSV, and that's the most likely reason. Commented Aug 29, 2019 at 11:27
  • 1
    You might use another delimiter. Depending on your locale setting Excel might reserve the default ',' (comma) for something else. Commented Aug 29, 2019 at 11:40
  • 1
    Your CSV and PowerShell code are fine; it's Excel that has the problem with the CSV. Did you check the "Highly Relevant" link I gave you? You may need to insert the BOM Commented Aug 29, 2019 at 11:50
  • 2
    Possible duplicate of How to correctly display .csv files within Excel 2013? Commented Aug 29, 2019 at 11:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.