Search code examples
powershelltext-parsing

How to export the start of a tenant name in PowerShell?


I need to create a CSV file and export it into a folder.

These are our requirements...

  • Folder name needs to be the Office 365 tenant name.
  • CSV file name needs to be tenant name.
  • Tenant name needs to be the bit before ".onmicrosoft.com".

This is how far I've got

$identity = Get-OrganizationConfig | Select identity
Export-Csv <path>\$identity.csv

The file name is "@{Identity=COMPANY.onmicrosoft.com}.csv.

How do I select the start of the tenant name?


Solution

  • The first issue is that Select-Object Identity creates a new object with an Identity property. If you just want the value of the Identity property, use Select-Object -ExpandProperty:

    $identity = Get-OrganizationConfig | Select -ExpandProperty Identity
    

    To remove the last part of the tenant FQDN, you could use the -replace regex operator:

    $identity = $identity -replace '\.onmicrosoft\.com$'
    

    or the String.Replace() method:

    $identity = $identity.Replace('.onmicrosoft.com')
    

    If you want to capitalize the first letter in the tenant name, you could use CultureInfo.TextInfo.ToTitleCase():

    $identity = (Get-Culture).TextInfo.ToTitleCase($identity)