r/PowerShell • u/Grant_Son • Sep 25 '26
Solved Powershell - exporting AD distinguished names to CSV
Solution:
Apparently the solution is not to change the delimiter and stick with commas, Excel is then smart enough to tel the difference.
Thanks everyone
I'm using get-adcomputer to export a list of PCs to a CSV.
I have set the delimiter in the exported CSV to be a semi colon,
When the CSV is opened in excel I end up with:
| A | B | C |
|---|---|---|
| Name;OSversion;created date;DN | ||
| PC1;windows11;25/09/26;CN=PC1 | OU=Desktops | DC=office |
[PSCustomObject]@{
Name = $($computer.Name)
OperatingSystem = $($OS)
WhenCreated = $($WhenCreated)
DistinguishedName = $($computer.DistinguishedName)if i try encasing each part of the custom object in escaped quotes e.g.
DistinguishedName = "`"$($computer.DistinguishedName)"`" the quotes and the backticks cary over to the CSV so column A ends up looking like "'"PC1"'";"'"windows11"'";"'"25/09/26"'";"'"CN=PC1 B & C are unaffected.
How do I stop export-csv or excel from splitting the DN?
*Update*
Sorry I though I had included the export-csv line below the custom object
$computerData |Sort-object -Property Name | export-csv $outputFile -NoTypeInformation -Delimiter ";"
13
u/Ecrofirt Sep 25 '26
Easiest answer? Change the delimiter.
Export-CSV -Delimiter `t
will produce a tab-delimited file. I switched to that years ago and it's been easy street since.
5
u/purplemonkeymad Sep 25 '26
Use the default comma as a separator. Export-csv will automatically wrap values in quotes, and excel will be able to read those commas as part of the data, not a delimiter ie:
will give you:
Which excel will open and correctly column.
Trying to place quotes into the data will just create extra stuff you don't want.
If you have another program that does not support quoted values, technically it does not support csv.
2
u/UserProv_Minotaur Sep 25 '26
I just ran it natively to export-csv, no delimiter set so it defaulted to commas, with a select/select object using the headers in the order that I wanted to be in the CSV - like
or
Sometimes even without the select or select-object.
Never had an issue with the columns for DN going rogue unless I did a text to columns with a semicolon delimiter.
2
u/Coffee_Ops Sep 25 '26
Actual solution: use
importExcelmodule andexport-xlsx -tableName ADStuff -tablestyle medium5Also just a plug for exporting canonicalName as well, this lets you actually sort the DNs lexically.
2
u/myrland Sep 25 '26
While you figured it out, for another time where you work with reports provided by other systems or colleagues: Open the CSV file in notepad or some editor and add this to the first line, then open it normally:
tab=;
Or whatever the delimiter is set to, it'll override the region-setting in Windows that Excel uses :)
1
u/ignescentOne Sep 25 '26
Change the delimiter, or replace the commas with something else in the dn when you drop it into the object, or force the quotes using a separate variable that has the quote in it instead of using the escape.
1
u/Carpathium Sep 25 '26
Why not just clean up the object or build a custom object that’s nice and clean and then dump that to csv?
1
u/NextVoiceUHear Sep 25 '26
Thank you 🙏 for that update info. So, then, if not a pipe (|) or a caret (^) what character can be used as a delimiter that’s not already part of some other something? How about the tilde “~” ??
3
u/NextVoiceUHear Sep 25 '26
As a DBA (retired) I found commas to be a terrible delimiter ‘cuz they appear so frequently in written text. Either of these (pipe & carat) are perfect because they do not:
| and ^
2
u/Semt-x Sep 25 '26
Excel uses the List seperator configured in Windows (start menu , type 'region', open the region menu, click "addittional settings" button at the bottom. there you'll see "List Seperator" which is now a comma.
change it to semicolon. then open the csv again. it wont splits the dn but uses ';' as sperator
2
u/jr49 Sep 25 '26
I've never had issues with excel splitting DNs when just using
1
u/Unholysmash Sep 25 '26
It looks like the object is being output correctly. Are
You adding the object to an array before exporting? Each attribute should be pulled over as a column name.
1
u/commiecat Sep 25 '26
Can you post the full sanitized script? My DNs show full in Excel.
If you ever want to list where the objects are in AD in an easier-to-read format, look at including the AD property
CanonicalName. I use this often with-Replace "/[^/]*$", ""to trim the object name, leaving only the path. This way we can visualize, sort, and filter AD locations in the CSV.In your above example, this would display as
domain.loc\office\Desktops