Get Username in PowerShell

I an trying to get the usernames and mailbox size corresponding to all mailboxes on an Exchange 2003 server.

I've got this:
Get-WMIObject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer r2d2 | sort-object MailboxDisplayName | Sort-Object Size -Descending | format-table MailboxDisplayName,Size

It works great, but I like to change the MailboxDisplayName to something that gives me the UserName/samAccountName instead.

Is it possible?
Kasper KatzmannSeniorkonsulentAsked:
Who is Participating?
 
Ernie BeekExpertCommented:
I once found this one:
Get-WmiObject -Class Exchange_Mailbox -NameSpace root\microsoftexchangev2 -ComputerName ExchangeServer | Sort-Object MailboxDisplayName | Format-Table MailboxDisplayName, @{Label='SamAccountName';Expression={(Get-QADUser -Identity $_.LegacyDN).SamAccountName}}, ServerName, StorageGroupName, StoreName, Size -auto

From: http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2009/07/16/mapping-a-wmi-mailbox-to-its-ad-user-account.aspx
0
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
Change MailboxDisplayName -> DistinguishName .... check the attribute and change it to it.

- Rancy
0
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
You can also have "cn" or "displayName" or "name".
All these attributes just have the Display Name :)

- Rancy
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
Kasper KatzmannSeniorkonsulentAuthor Commented:
It only gives blank fields. I have used both DistinguishName and DistinguishedName.
0
 
Kasper KatzmannSeniorkonsulentAuthor Commented:
Ok, if I put in Format-List instead of Format-Table, I get the some good info. I can see that the LegacyDN in fact holds the username. As in:

LegacyDN : /O=COMPANY NAME/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=ELS

Is there a way to strip the output abowe, so that I only get the last three letters and if one of those three letters are a "=" then remove it?
0
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
These 3 attributes in AD have the "Display Name" something like "Last, First" and so is that wahat you want ? (( "cn" or "displayName" or "name" ))

What you have in the Legacy Exchaneg DN could be either DisplayName or Alias.

- Rancy
0
 
Kasper KatzmannSeniorkonsulentAuthor Commented:
No, it's the username/loginname i need
0
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
mailNickname - UserID

msDS-PrincipalName - DomainName\UserID

I guess this is what your looking for ? Just to let you know i am checking this in ADSIEDIT on W2k8 with E2k10 .... so if you have issues you can check with ADSIEDIT for an attribute on a user to understand what attribute you want :)

- Rancy
0
 
Kasper KatzmannSeniorkonsulentAuthor Commented:
Dosn't work either.
I guess the problem is that I'm using Get-WMIObject instead of calling the AD.
0
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
Get-QADUser is for AD .... maybe this will work

- Rancy
0
 
Kasper KatzmannSeniorkonsulentAuthor Commented:
@erniebeek - I used your suggestion, but I had to change it a bit, to make it work.

Get-QADUser was changed to Get-User and then... viola

Get-WmiObject -Class Exchange_Mailbox -NameSpace root\microsoftexchangev2 -ComputerName r2d2 | Sort-Object
 size -descending | Format-Table @{Label='SamAccountName';Expression={(Get-User -Identity $_.LegacyDN).SamAccountName}},
Size -auto
0
 
Ernie BeekExpertCommented:
I was just looking the other way...........
But you managed to get it to work :)

Glad I could help you in the right direction and thx 4 the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.