Well this would probably be better written as a function. But, your ForEach construct isn’t part of a pipeline. It’s a scripting statement. It’s actually exporting all of them - it’s just, each time, it overwrites the previous file, so to you it only looks like the last one worked.
Well, that’s basically why I wrote “Learn PowerShell Toolmaking in a Month of Lunches.” Which I heartily recommend if you want to learn to do this the right way with a lot less work.
Function Get-MyStuff {
do your stuff here - don’t export it to CSV, just let it output to the pipeline
basically put your code here but delete the export-CSV and the pipe right before it
}
Get-MyStuff | Export-CSV filename.csv
Your code is working just fine. It’s just that each domain results in a new CSV file, which happens to be the same as the previous CSV file name, so it just overwrites it.
Yes I read that book cover to cover and helped with all the typos, remember? and then bought all of Manning Presses books too. I’ll get there I just have to reconcile actual work with text book examples and find it takes time. I appreciate your patience and I do search to forum first for my elementary questions but most are quite advanced still for me.
That little template I posted above should do what you need - give that a shot as a starting point. I mainly want to make sure you know why your existing code isn’t working - so if that’s not clear, let me know.
Like, if you used a variable of some kind instead of output.csv for the filename, and you changed that variable each time through, you’d get multiple CSV files with everything you want. You’re that close. Format-list works because it doesn’t “overwrite the existing file” like Export-CSV does.
Heck, just add -Append to your Export-CSV command and you’ll get something closer to what you want. It’s more important to me that you understand why, though - so please, ask if it’s not clear!