Home > Software design >  Sorting Office 365 user account / mailbox properties
Sorting Office 365 user account / mailbox properties

Time:12-27

I'm accessing my cloud Office 365 Exchange Server via Powershell. Showing all properties of an account can be done via Get-Mailbox 'username' | Select * (MS reference).

On most systems those properties are already sorted (e.g. Get-ADUser 'username' -Properties *). Is it possible to sort the Get-Mailbox output? I thought Get-Mailbox 'username' | Select * | Sort-Object would do the trick but it didn't make any difference (I guess a property doesn't constitute an object). What's the right command to sort the properties for a single user?

Note: Sorting properties of multiple accounts works fine e.g. Get-Mailbox -filter * | select Name, DisplayName | Sort-Object Displayname

Update: I managed to get a sorted properties list using

(Get-Mailbox 'mailboxname' | select *).PSObject.properties | ForEach-Object {$_.Name} | Sort-Object Name

it gets me the following output:

AcceptMessagesOnlyFrom
AccountDisabled
AddressBookPolicy
ArchiveWarningQuota
...

$_.Name gives me the values but so far I couldn't combine both into one list, I'm trying to get s.th. like this:

AcceptMessagesOnlyFrom = {}
AccountDisabled = False
AddressBookPolicy = 
ArchiveWarningQuota = 45 GB (48,318,382,080 bytes)
...

CodePudding user response:

I'm not completely sure this is what you are after, let me know if I'm wrong. As in my comment, Sort-Object can handle sorting a list or an object[] by one or more of it's properties; but sorting one single object by it's properties, say alphabetically, would require a combination of accessing the object's properties with .PSObject.Properties.Name and then sorting this list with Sort-Object. And after that we can use Select-Object with this sorted list to display the object as we want.

Using the object below as an example as I have no idea how of the type Microsoft.Exchange.Data.Directory.Management.Mailbox looks.

$mailbox = [pscustomobject]@{
    DisplayName = 'someuser'
    UserPrincipalName = '[email protected]'
    Mail = '[email protected]'
    IsLicensed = $true
}

$properties = $mailbox.PSObject.Properties.Name | Sort-Object

$mailbox | Select-Object $properties

As you can see, object's properties are now sorted alphabetically:

DisplayName IsLicensed Mail                    UserPrincipalName
----------- ---------- ----                    -----------------
someuser          True [email protected] [email protected]

By looking at your edit, seems like you are looking for a one-liner, so this is how it could look:

Get-Mailbox 'mailboxname' | ForEach-Object {
    $_ | Select-Object ($_.PSObject.Properties.Name | Sort-Object)
}
  • Related