Question

When I'm building a CSV Export it puts quotes around every set of parameters. However, I only want 1 set to have the quotes.

$UserInfo = New-Object System.Object
foreach ($user in $MailBoxList)
{
  $UserInfo | Add-Member -Type NoteProperty -Name LegacyExchangeDN -Value $user.LegacyExchangeDN
  $UserInfo | Add-Member -Type NoteProperty -Name CloudEmailAddress -Value $CloudEmailAddress
  $UserInfo | Add-Member -Type NoteProperty -Name OnPremiseEmailAddress -Value $user.PrimarySMTPAddress.ToString()
  $UserInfo | Add-Member -Type NoteProperty -Name MailboxGUID -Value $user.ExchangeGUID

  $Users += $UserInfo
}

$Users | Export-CSV -Delimiter "," -Path ".\cloud.csv" -NoTypeInformation

This code results in this exported CSV:

"LegacyExchangeDN","CloudEmailAddress","OnPremiseEmailAddress","MailboxGUID"
"/o=MyUnit/ou=This Admin Group (BRBIDOMF87SRQLT)/cn=Recipients/cn=a0dd27c5djd864108cfa61dj37dj56c6-MyLastName, M","mMyLastName@WhereverImFrom.Bob.com","mMyLastName@AnotherDomain.com","687efe5e-4690-110e-86a5-69fr4cdecf7e"

What I'd like is just the first column of data to have double quotes and the rest without:

"/o=MyUnit/ou=This Admin Group (BRBIDOMF87SRQLT)/cn=Recipients/cn=a0dd27c5djd864108cfa61dj37dj56c6-MyLastName, M",mMyLastName@WhereverImFrom.Bob.com,mMyLastName@AnotherDomain.com,687efe5e-4690-110e-86a5-69fr4cdecf7e

Any thoughts?

Was it helpful?

Solution

You can "roll your own" csv file:

$Outfile = ".\cloud.csv"
'LegacyExchangeDN,CloudEmailAddress,OnPremiseEmailAddress' | Set-Content $Outfile

foreach($user in $MailBoxList)
{
  $Data = @(
             $user.LegacyExchangeDN,
             $CloudEmailAddress,
             $user.PrimarySMTPAddress.ToString()
            )

 '"{0}",{1},{2}' -f $Data | Add-Content $Outfile
}

OTHER TIPS

Here's one solution. What it does is convert the objects to csv-strings and then modify them before saving the csv to a file.

foreach($user in $MailBoxList)
{
    $UserInfo = New-Object System.Object
    $UserInfo | Add-Member -Type NoteProperty -Name LegacyExchangeDN -Value $user.LegacyExchangeDN
    $UserInfo | Add-Member -Type NoteProperty -Name CloudEmailAddress -Value $CloudEmailAddress
    $UserInfo | Add-Member -Type NoteProperty -Name OnPremiseEmailAddress -Value 
    $user.PrimarySMTPAddress.ToString()

    $UserInfo | Add-Member -Type NoteProperty -Name MailboxGUID -Value $user.ExchangeGUID

    $Users += $UserInfo

}

#Convert to csv-formatted-strings
$csv = $Users | ConvertTo-Csv -NoTypeInformation -Delimiter ","

#Get header
$out = @($csv[0])

#Remove quotes for all but first column
$out += $csv[1..($csv.Length-1)] | % {

    [regex]::Match($_,'(^"[^"]*",)(.*)') | % { "{0}{1}" -f $_.Groups[1], $_.Groups[2].Value.Replace('"',"") }

}

$out | Set-Content -Path ".\cloud.csv"

Another apporach without regex:

#Remove quotes for all but first column
$out += $csv[1..($csv.Length-1)] | % {

    $values = $_ -split '","'
    $values[0] + '"' + $values[1..($values.Length-1)] -join ','

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top