Exchange Powershell help for a noob

Associate
Joined
26 Nov 2004
Posts
980
Location
Carshalton, Surrey
OK so basically what i want to do is create a csv file with the diplayname, server, Alias and size of a set of users from a csv file

$ukstats = Import-Csv "D:\PS_scripts\data_files\Alpha\teststats-uk.csv"
$ukstats | foreach {
Get-MailboxStatistics -identity $_.name | select displayname, servername, Alias, totalitemsize | Export-Csv D:\PS_scripts\data_files\Alpha\mailboxresultsuk.csv
}

This is what I have above, the problem I have is that it creates the export but only has the last user from the import file. Why is it not doing it for each user in turn? I don't really do the powershell side of things that's my colleague but he's on holiday for two weeks and as is always the case management want this info asap.
 
Soldato
Joined
25 Mar 2004
Posts
15,902
Location
Fareham
Seems a bit random importing a CSV to then export to another CSV, but who knows?

PHP:
$ukstats = Import-Csv "D:\PS_scripts\data_files\Alpha\teststats-uk.csv"
$Array = @()
$ukstats | foreach {
$Array += Get-MailboxStatistics -identity $_.name | select displayname, servername, Alias, totalitemsize
}
$Array | Export-Csv "D:\PS_scripts\data_files\Alpha\mailboxresultsuk.csv"
 
Associate
OP
Joined
26 Nov 2004
Posts
980
Location
Carshalton, Surrey
Thanks for that i will give it a try, as to the csv import that's probably just me not knowing a better way of doing it. I tried the get-content command with it pointing to a txt file but could not get it to work.

Essentially the reason behind it is that i only need to get the information for a subset of our users and the csv well its actually just a txt file, is just populated with the usernames of those users in a single column.
 
Soldato
Joined
25 Mar 2004
Posts
15,902
Location
Fareham
I guess depending upon what you're doing, assuming the users in question could be stored in an OU, you could just do like:

Get-Mailbox -OrganizationalUnit "OU Path" | Get-MailboxStatistics | Select Whatever | Export-Csv -NoType "Path to export.csv"
 
Associate
OP
Joined
26 Nov 2004
Posts
980
Location
Carshalton, Surrey
The users are stored in various different OU's and no possibility of moving them, my company has sold part of the business and the list i have is those employee's that will be tuped over to the new company, the list is based on their employee number which is also the username so I have the export from the HR DB of those that will be going, hence the reason for the imported csv.

Your first one has worked perfectly thank you, one more question if I may if I need to get results from both get-mailbox and mailboxstatistics how would that work with your first suggestion.

For example if i needed alias and distinguished name which are not available with mailboxstatistics.
 
Soldato
Joined
25 Mar 2004
Posts
15,902
Location
Fareham
Actually I'd spin this on it's head, most of the interesting fields are in Get-Mailbox, Get-MailboxStatistics is mainly good for finding out how big a mailbox is.

There are more than one ways to do this, you could build an array that contains elements of both sets (Get-Mailbox with Get-MailboxStastics). You could get back Get-Mailbox and then add member noteproperties for fields from Get-Mailboxstatistics.

This is a quick and dirty way to do it though, the main downside is that you may wind up calling Get-MailboxStatistics more than once per user. Processing time wouldn't necessarily be great if your list was extremely long.

Try it out and let me know, I am just using an in-line expression to grab the Mailbox size each time. You can add more fields from Get-Mailbox, or copy what I did for the MailboxSize to get more fields from the MailboxStatistics.

PHP:
$ukstats = Import-Csv "D:\PS_scripts\data_files\Alpha\teststats-uk.csv"
$Array = @()
$ukstats | foreach {
$Array += Get-Mailbox $_.Name | Select PrimarySmtpAddress, Database, UserPrincipalName, @{Name="MailboxSize";Expression={(Get-MailboxStatistics -Identity $_.Identity).TotalItemSize.Value.ToMb()}}
}
$Array | Export-Csv "D:\PS_scripts\data_files\Alpha\mailboxresultsuk.csv"
 
Associate
OP
Joined
26 Nov 2004
Posts
980
Location
Carshalton, Surrey
Thanks very much for the help that is perfect gives me the ability to add in more info when management inevitably come back asking for more. I only have about 700 users to collect the data from for this round so speed should be pretty quick i think.
 
Associate
Joined
17 Aug 2008
Posts
19
Part of the problem with the code you have here besides there are many ways of cracking the egg, is that you need to append the file that your are exporting to because on each run you overwriting the original output.

so on the export you just need the -append and -notypeinformation switch.

OK so basically what i want to do is create a csv file with the diplayname, server, Alias and size of a set of users from a csv file

$ukstats = Import-Csv "D:\PS_scripts\data_files\Alpha\teststats-uk.csv"
$ukstats | foreach {
Get-MailboxStatistics -identity $_.name | select displayname, servername, Alias, totalitemsize | Export-Csv D:\PS_scripts\data_files\Alpha\mailboxresultsuk.csv
}

This is what I have above, the problem I have is that it creates the export but only has the last user from the import file. Why is it not doing it for each user in turn? I don't really do the powershell side of things that's my colleague but he's on holiday for two weeks and as is always the case management want this info asap.
 
Last edited:
Back
Top Bottom