Splitting and filtering strings

This topic contains 2 replies, has 2 voices, and was last updated by  Andrew Cheal 5 years, 2 months ago.

  • Author
  • #4780

    Andrew Cheal

    I need some help!

    I have a one-liner that extracts specific user data from AD and saves it in to a CSV file.  One of those attributes is proxyAddresses from which I only need to extract the primary SMTP and X400 addresses i.e. where X400 and SMTP are in uppercase. The addresses are separated in the string by a space which needs replacing by a semi-colon.  Any idea how I can achieve this?

    get-ADobject -filter 'displayname -like "XYZ*" -and mail -like "*@*"' -properties sn,givenName,DisplayName,title,mail,physicalDeliveryOfficeName,telephoneNumber,facsimileTelephoneNumber,proxyAddresses |
    Select-Object sn,givenName,displayName,physicalDeliveryOfficeName,telephoneNumber,facsimileTelephoneNumber,title,mail,BuildingCode,@{name="Proxies";expression={[string]$_.proxyAddresses}} |
    ConvertTo-Csv -NoTypeInformation | foreach {$_ -replace '"', ''} | out-file $exportFile
  • #4792

    Rob Campbell

    You're casting it to [string] to unroll the array, and that's separating the members with a space. Powershell has an automatic variable called $OFS (for Output Field Separator) that determines what separator character(s) will be used when you do that. The default value is a space. If you change that to a semi-colon before you run that script, it will use that instead of the space.

    $OFS = ';'

  • #4824

    Andrew Cheal

    Thanks for that Rob, will check that out

You must be logged in to reply to this topic.