r/PowerShell • u/Minute_Evidence_4244 • Mar 25 '26
Get-ADComputer output to CSV as a text file does not look like what is showing in Excel.
(EDIT) Thank you all for the input. Powershell is not something I have ever worked with before and a while back I found out about Get-ADComputer and that list helped me with my normal duties using Excel. I just recently decided to try to automate reformatting it, and removing all the elements I don't need by parsing it. I have never run into "that I know of" the double line behavior and it threw me for a loop. Now that I know what the issue is, I can move forward.
Again, thank you for the great information, and timely response.
(End)
Most show correctly, such as
mot.sz0144/WinServ Backup Server/Z001S001LBS01 10.1.1.71 10/7/2023 15:52
But some shows up like
"mot.sz0144/Site Containers Hosts/z001s001rfe02
CNF:65a00800-62ab-4225-923d-619d0080371c" 10.101.1.229 10/23/2023 10:30
When I open the CSV in Excel everything looks great.
I am trying to parse this output, what should be a single line gets broken into 2.
I am generating the report with the following.
Get-ADComputer -Filter * -properties canonicalname,IPv4Address,LastLogonDate | select canonicalname,IPv4Address,LastLogonDate | Export-CSV c:\computers_by_OU.csv -NoTypeInformation
7
u/Usual-Chef1734 Mar 25 '26 edited Mar 25 '26
Use the Import-Excel module instead. You can control a ton of row level formatting and things like that. I don't have a legacy AD environment (acutally I do but just got access from the client) but anyway you just have special characters (the slashes most likely) that are getting treated like /n or /cr because of the encoding. That is how I like doing it , but if you are in a pinch and can't install modules for easier use you can calculate those fields that are troublesome like 'canonicalname'.
```pwsh
Get-ADComputer -Filter * -Properties canonicalname, IPv4Address, LastLogonDate | Select-Object @{Name='canonicalname'; Expression={$_.canonicalname -replace "[\r\n]", ""}}, IPv4Address, LastLogonDate | Export-CSV c:\computers_by_OU.csv -NoTypeInformationthat should do it.
5
u/NellovsVape Mar 25 '26
I don't get where is the problem, can you word it differently?
1
u/Carribean-Diver Mar 25 '26
The CNF:* AD objects are created when conflicting updates are simultaneously (or nearly so) made on different AD controllers. It may be a symptom of suboptimal AD infrastructure design.
1
u/robwe2 Mar 25 '26
Try en -encoding UTF8 at the end of
2
u/lethargy86 Mar 25 '26
I guess the CN somehow contains a CR/LF in some cases
Your parsing might need to use some clever regex to account for it, or you can do some regex replace to clear those out before parsing. Dealer’s choice
edit been a while since I looked but there is probably a nuget csv parser that can be set-up to parse multilines correctly
1
u/AppIdentityGuy Mar 25 '26
Have you tried a join command??
1
u/arslearsle Mar 26 '26
You could re-parse the input quite simple…
But there prob is a solution for it in powershell gallery/github etc
1
u/jimb2 Mar 27 '26 edited Mar 27 '26
I do a lot of this. Exporting AD data is fraught because of the data types. The best general way handle is is to convert each record to a pscustomobject. Here's one I prepared earlier:
This looks a bit log winded but PSCusomObject seems to reliably play nice with Export-Csv. You can put this code pattern in a pipe like using % (foreach-object). I tend to write little scrips with a bit of progress commentary and basic error checking (eg, nothing found) rather than piping. A pipe is a black box.
If the attribute is an array you need to some kind of text join, like in the orgunit above. If it's something weirder, do a string conversion or whatever smarts in your code. This means you get nice output. If you try to add even a bit of data wrangling in a select-object, you end up with ugly code fast.
Also, as mentioned elsewhere, get the ImportExcel module. It's much better for distributing data to people. I can add some useful preset parameters in my profile to freeze the column headings and size the columns to fit the data:
Export code:
1
u/Rexon2 Mar 26 '26
This is a classic CSV parsing gotcha. The issue is that some of your AD objects have embedded newline characters in their
canonicalnameproperty (thoseCNF:entries are Active Directory conflict/phantom objects). WhenExport-Csvwrites them out, it correctly wraps the field in quotes per the CSV spec — Excel handles this fine, but naive line-by-line text parsing breaks.A few ways to fix this:
1. Clean the data before export (easiest — strip the newlines):
powershell
2. Parse the CSV properly instead of treating it as text:
If you're reading the file back in PowerShell, use
Import-Csvinstead ofGet-Content— it handles quoted multi-line fields correctly:powershell
3. Filter out the conflict objects entirely (you probably don't want them anyway):
powershell
0
u/CarrotBusiness2380 Mar 25 '26
How are you reading the file? Why are you not using
Import-Csvto parse it?