# Adding a split string to CSV

**URL:** <https://forums.powershell.org/t/adding-a-split-string-to-csv/25303>\
**Category:** PowerShell Help\
**Created:** [January 13, 2025, 7:05pm UTC](https://forums.powershell.org/t/adding-a-split-string-to-csv/25303 "2025-01-13T19:05:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xml](https://avatars.discourse-cdn.com/v4/letter/x/96bed5/32.png) [@xml](https://forums.powershell.org/u/xml)\
**Post date:** [January 13, 2025, 7:05pm UTC](https://forums.powershell.org/t/adding-a-split-string-to-csv/25303/1 "2025-01-13T19:05:32Z")

</div>

I am a bit confused on the most efficient way to achieve this task. In exchange, I’m attempting to generate a csv that holds the data I need. Below is the command I am using:

```auto
Get-Mailbox -ResultSize 20 | Select-Object "UserPrincipalName", "Alias" | Export-Csv "C:\Users\xml"

```

The format for “alias” is always “firstname.lastname”. In my csv file, I’d like additional fields added under “FirstName” and “LastName”, and I was going to populate these fields by splitting the “Alias” string, and last name to the $lastname=@().

Something like this:

```auto
$firstname=@()
$lastname@()
foreach($alias in $mailboxes){
$firstname += $alias.split"."[0]
$lastname += $alias.split"."[1]

```

As I was writing this I realized I could probably just build a PS custom object and feed each value in the value, then export. Any suggestions would be appreciated though.

EDIT: So after trying

```auto
$Mailboxes = Get-Mailbox -ResultSize 20 | Select-Object "UserPrincipalName", "Alias"

foreach($alias in $Mailboxes.alias){
    $name = $alias -split "."
    $firstname = $name[0]
    write-host $firstname
}

```

This doesn’t output anything ☹

---

<div class="post-metadata">

**Author:** ![LearnLaughOps](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/learnlaughops/32/5688_2.png) [@LearnLaughOps](https://forums.powershell.org/u/LearnLaughOps)\
**Post date:** [January 13, 2025, 7:33pm UTC](https://forums.powershell.org/t/adding-a-split-string-to-csv/25303/2 "2025-01-13T19:33:03Z")

</div>

Try using calculated properties:

```auto
Get-Mailbox -ResultSize 20 | Select-Object UserPrincipalName,
                                            @{
                                                name='FirstName'
                                                expr={$_.Alias.split(".")[0]}
                                            },
                                            @{
                                                name='LastName'
                                                expr={$_.Alias.split(".")[1]}
                                            } | Export-Csv "C:\result.csv"

```

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [January 13, 2025, 7:40pm UTC](https://forums.powershell.org/t/adding-a-split-string-to-csv/25303/3 "2025-01-13T19:40:43Z")

</div>

I think @LearnLaughOps solution is the best, and it’s a technique you’ll find a lot of use for.  
Here’s a long winded way of achieving something similar.

```Powershell
$MailBoxes = Get-Mailbox -ResultSize 20

$Users = Foreach ($Mailbox in $Mailboxes) {
    [PSCustomObject]@{
        UserPrincipalName = $Mailbox.UserPrincipalName
        Alias = $Mailbox.Alias
        FirstName = $Mailbox.Alias.split('.')[0]
        LastName = $Mailbox.Alias.split('.')[1]
    }
}

$Users | Export-Csv c:\Users\report.csv -NoTypeInformation

```

First thing is to capture all the mailboxes in a variable. I’m foregoing Select-Object here which means in the future if there are more properties I want to include in my export they’ll still be on those objects and I can just add another property to my PSCustomObject.

Then we loop through the mailbox objects and for each one, we output a PSCustomObject with the property definitions we want, including splitting the Alias in to two new properties: FirstName and LastName.

Then all that output is being captured in a variable. This gives me a chance to review it in VS Code and check that the output is what I want.

Lastly, the Users variable is piped to Export-Csv where I’m also using the `-NoTypeInformation` or `-nti` (for short) to exclude the first row that’s usually output.

---

<div class="post-metadata">

**Author:** ![xml](https://avatars.discourse-cdn.com/v4/letter/x/96bed5/32.png) [@xml](https://forums.powershell.org/u/xml)\
**Post date:** [January 13, 2025, 7:57pm UTC](https://forums.powershell.org/t/adding-a-split-string-to-csv/25303/4 "2025-01-13T19:57:03Z")

</div>

Your answer used the PSCustomObject, which I needed to use to change the header values of “UserPrincipalName” and “Alias”. I’m still learning to use this function so I appreciate your response.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [February 12, 2025, 7:58pm UTC](https://forums.powershell.org/t/adding-a-split-string-to-csv/25303/5 "2025-02-12T19:58:03Z")

</div>

Closed on February 12, 2025, 7:58pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
