Add array collections to cell in CSV

BlackCat 81 Reputation points
2021-08-04T00:10:40.383+00:00

I am using Get-ADDomain to have list of child domain
$DomainInfo = Get-ADDomain $env:USERDNSDOMAIN
$ChildDomains = $DomainInfo.ChildDomains

This will give me all child domains in format "child1.int child2.int child3.int". I want to export result to CSV with headers and all child domains will add to column header "CHild Domains".

This is my code to create CSV with headers
$obj = [pscustomobject]@{

FQDN = $ForestInfo.name

ForestMode = $ForestInfo.Forestmode

SchemaMaster  = $ForestInfo.SchemaMaster

DomaininForest = $ForestInfo.Domains.Count

   ChildDomains = "$ChildDomains"

   ChildCount = $ChildDomains.Count

This is K for me but all child domains are showed as one line "child1.int child2.int child3.int" at column "Child Domains". I want to format it as

Child Domains
child1.int
child2.int
child3.int

How can I archive this?

Thanks

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.
5,405 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,111 Reputation points
    2021-08-04T02:14:52.027+00:00

    The short answer is that you can't.

    A CSV has no concept of formatting.

    You can get Excel to do it though. To put multiple values in the same cell you need to separate them, so something like this may work for you by changing line #9 in you example:

    ChildDomains = $ChildDomains -join "`n"
    

    Import the result into Excel and get the cell height big enough to show "x" number of values.


0 additional answers

Sort by: Most helpful