Search code examples
powershellexport-to-csv

convert powershell object to a single field in a CSV


The code below works perfectly fine to view the data on my screen but if I want to export it to a csv the fields for userRules and Last10email are blank. How would I convert the objects $userRules and $last10Email into blocks of text that will keep their formatting in my CSV (IE I want the whole table in a single field of the CSV)? I poked GPT with this but it just had me running in circles with suggestions that didn't work.

        # Get inbox rules for the user
    $userRules = Get-InboxRule -Mailbox $username | select Name, enabled, MoveToFolder, DeleteMessage, SoftDeleteMessage | Format-Table -AutoSize | Out-String

    # Get the last 10 emails for the user
    $last10EmailUTC = Get-MessageTrace -SenderAddress $User -PageSize 10 |
        Select-Object -Property Received, SenderAddress, RecipientAddress, Subject |
        Sort-Object -Property Received -Descending | Format-Table -AutoSize | Out-String

    # Convert received timestamps to local time (PST)
    $last10Email = $last10EmailUTC | ForEach-Object {
        if ($_.Received -ne $null) {
            $_.Received = $_.Received.ToLocalTime().AddHours(-8)  # Subtract 8 hours for PST
        }
        return $_
    }

# Create and return a custom object
[PSCustomObject]@{
    userPrincipalName     = $user
    SamAccountName     = $userdetails.SamAccountName
    Name                  = $userdetails.Name
    employeeId             = $userdetails.employeeId
    Enabled               = $userdetails.Enabled
    localbadPwdCount           = $userdetails.BadLogonCount
    LastlocalBadPasswordAttempt = $userdetails.LastBadPasswordAttempt
    LastLocalLogonDate         = $userdetails.LastLogonDate
    PasswordExpired       = $userdetails.PasswordExpired
    PasswordLastSet       = $userdetails.PasswordLastSet
    userRules            = $userRules
    Last10email          = $last10Email

}

Solution

  • Your code is correct, you're already using Format-Table -AutoSize | Out-String to have the string representation of the table, the issue as confirmed in comments, is due to Out-String adding extra lines to your output.

    You can solve this problem by trimming the outputted string:

    [PSCustomObject]@{
        userPrincipalName           = $user
        SamAccountName              = $userdetails.SamAccountName
        Name                        = $userdetails.Name
        employeeId                  = $userdetails.employeeId
        Enabled                     = $userdetails.Enabled
        localbadPwdCount            = $userdetails.BadLogonCount
        LastlocalBadPasswordAttempt = $userdetails.LastBadPasswordAttempt
        LastLocalLogonDate          = $userdetails.LastLogonDate
        PasswordExpired             = $userdetails.PasswordExpired
        PasswordLastSet             = $userdetails.PasswordLastSet
        userRules                   = $userRules.Trim()                  # here
        Last10email                 = ($last10Email | Out-String).Trim() # and here
    }