Hi,

I’m currently trying to make a script that shows us for each exchange user how big their mailbox is. We will soon start a migration to another exchange server. It also shows me when they last logged in to thair accounts etc …

I would like to include an extra column, showing me from what company the user is. It is realy giving me a headache.

This is what I have so far:

Get-MailboxDatabase | get-mailboxstatistics | sort-object -property totalitemsize -descending | select-object displayname, itemcount, @{ expression={$_.TotalItemSize.Value.ToKB()} } , lastloggedonuser, lastlogontime, lastlogofftime, servername, databasename | export-csv “c:/MailboxStats-sizes.csv”

Thanks in advance!

3 Spice ups

Hello,

this is a powershell question

If you post code, please use the ‘Insert Code’ button. Please and thank you!codebutton_small.png

Where does it show you in Exchange from what company the user is?

What do you mean by ‘what company’ ? Generally, Exchange is for an organisation’s email (or a shared tennant in a hosted version but still one org) so the company is yours.

1 Spice up

Hi, Thanks for the replies. I mean that u can add a company name to a user as a prototype. It is a get-user property. I now wat to add this information in a extra column so I can see which users belongs to which company

We are a company that rents out mailaddresses. We have like 50 companies subscribed to our exchange service

Gotcha

# to set it
get-user $username | set-user -Company "test"

# to get it in your code
Get-MailboxDatabase | get-mailboxstatistics | sort -property totalitemsize -descending | 
select displayname, itemcount, @{ expression={$_.TotalItemSize.Value.ToKB()} } , 
lastloggedonuser, lastlogontime, lastlogofftime, servername, databasename,
@{n='Company';e={(get-user $_ ).Company}} | 
export-csv "c:/MailboxStats-sizes.csv"
1 Spice up

I see.

Where specifically do you store the company name?

Effectively what you need to do is:

select-object displayname, itemcount, 
    @{ expression={$_.TotalItemSize.Value.ToKB()} } , lastloggedonuser, lastlogontime, 
    lastlogofftime, servername, databasename,
    COMPANY

Where Company is the attribute name you use to store the company identifier. If that company is embedded in some other attribute, then you’d replace the COMPANY with a calculated property that derives the company name.

1 Spice up

Not tested but give a try

$outtbl = @()
$users = Get-User -ResultSize Unlimited | Where { $_.RecipientType -eq ‘UserMailbox’ }
$users | Foreach {
  $x = Get-MailboxDatabase | get-mailboxstatistics
  $t = New-Object PSObject -Property @{
    Company=         $_.company
    Name=            $_.name
    TotalItemSize=   $x.TotalItemSize.Value.ToKB()
    itemcount=       $x.itemcount
    lastloggedonuser=$x.lastloggedonuser
    lastlogofftime=  $x.lastlogofftime
    databasename=    $x.databasename
    LastLogonTime =  $x.LastLogontime
  }
  $outtbl += $t
}

$outtbl |sort -property totalitemsize -descending| Export-Csv c:\MailboxStats-sizes.csv -NoTypeInformation

Thanks for the reply JitenSh

Unfortually I get the following error:

You cannot call a method on a null-valued expression.
At C:\scripts\mailboxsizes.ps1:8 char:49
+     TotalItemSize=   $x.TotalItemSize.Value.ToKB <<<< ()
    + CategoryInfo          : InvalidOperation: (ToKB:String) [], RuntimeExcep
   tion
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\scripts\mailboxsizes.ps1:8 char:49
+     TotalItemSize=   $x.TotalItemSize.Value.ToKB <<<< ()
    + CategoryInfo          : InvalidOperation: (ToKB:String) [], RuntimeExcep
   tion
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\scripts\mailboxsizes.ps1:8 char:49
+     TotalItemSize=   $x.TotalItemSize.Value.ToKB <<<< ()
    + CategoryInfo          : InvalidOperation: (ToKB:String) [], RuntimeExcep
   tion
    + FullyQualifiedErrorId : InvokeMethodOnNull

Have you tried what I posted ?

@mikelbz

@Neally

Yes I just tried it but I it doesnt add the company in the csv file :frowning:

I haven’t executed the get-user set company cmdlet because i’m afraid he would overwrite anyone that already has the name added to his AD property.

I have executed the get information (which returned 0 errors) but it didnt add the company name to it.

A Get-* Cmdlet does ot over write anything. It just GETs the data. only SET-Cmdlets update things.

Did you try the change to the Select-Object I suggested above?

Sorry I meant the Set-cmdlet.

to set it get-user $username | set-user -Company “test”

which version of PowerShell are u running? it’s working for me even set-user

Try it with AD Powershell

get-aduser $username |set-aduser -company "test"