Link to home
Start Free TrialLog in
Avatar of Kasper Katzmann
Kasper KatzmannFlag for Denmark

asked on

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?
Avatar of Manpreet SIngh Khatra
Manpreet SIngh Khatra
Flag of India image

Change MailboxDisplayName -> DistinguishName .... check the attribute and change it to it.

- Rancy
ASKER CERTIFIED SOLUTION
Avatar of Ernie Beek
Ernie Beek
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also have "cn" or "displayName" or "name".
All these attributes just have the Display Name :)

- Rancy
Avatar of Kasper Katzmann

ASKER

It only gives blank fields. I have used both DistinguishName and DistinguishedName.
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?
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
No, it's the username/loginname i need
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
Dosn't work either.
I guess the problem is that I'm using Get-WMIObject instead of calling the AD.
Get-QADUser is for AD .... maybe this will work

- Rancy
@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
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.