Need help displaying data in columns rather than lists powershell v2

Posted on 2012-08-12
Last Modified: 2012-08-31
I have a script below and it is functional. The problem is it displays in lists instead of columns. I have tried to display with the columns but It is not going well and please help. Also please have mercy on me I am in no way an expert.

As you can see I am running the get mailbox  command to get all the objects and then running hte commands and picking certain fields. It is functional but I would rather have something like this displaying

Name of user   samaccountname  ou totalmailboxsize etc  etcc

---------------   -------------------  --  ------------------

Any help would be greatly appreciated

Get-mailbox  -resultsize unlimited |

 foreach {

 $mailboxid = get-mailbox -id $_
 $mbxstatistics = Get-Mailboxstatistics -id $_ | select databasename, DisplayName,LastLogonTime, @{n='Totalitemsize';e={
    If ($_.TotalItemSize.Value.ToBytes() -gt 1GB) {
    } ElseIf ($_.TotalItemSize.Value.ToBytes() -gt 1MB) {
    } Else {
    } }}, ItemCount
        $fldstats = Get-Mailboxfolderstatistics -id $_ |
    select-object name, @{n='Foldersize';e={
    If ($_.Foldersize.ToBytes() -gt 1GB) {
    } ElseIf ($_.Foldersize.ToBytes() -gt 1MB) {
    } Else {
    } }},itemcount

# this was used to test and make sure that all is working > select-object $mailboxid, $mbxstatistics

$combinecommands = new-object PSOBject
$combinecommands  | add-member -type noteproperty -name "Name of user" -value $mailboxid.displayname
$combinecommands  | add-member -type noteproperty -name "name of account" -value $mailboxid.samaccountname
$combinecommands  | add-member -type noteproperty -name "OU"      -value $mailboxid.organizationalunit
$combinecommands  | add-member -type noteproperty -name "Database"     -value $mbxstatistics.databasename
$combinecommands  | add-member -type noteproperty -name "size of mb"   -value $mbxstatistics.totalitemsize
$combinecommands  | add-member -type noteproperty -name "number of items" -value $mbxstatistics.itemcount
$combinecommands  | add-member -type noteproperty -name "Last logon time" -value $mbxstatistics.LastLogonTime

$combinecommands | select displayname, samaccountname | out-file c:\somefile.txt -append

Question by:techdrive
    LVL 7

    Expert Comment

    by:Casey Weaver
    I would use Format-Table for this. Microsoft has a nice explanation of the formatting options available in PS outputs.

    It's been about 1.5 years since I wrote powershell for Xenserver apps, but I think you would utilize it this way:

    $combinecommands | select displayname, samaccountname | Format-Table -Property displayname,sameaccountname -Autosize | out-file c:\somefile.txt -append
    LVL 4

    Expert Comment

    You can always try

    Get-Mailbox | Ft DisplayName, samaccountname, ou > Mailboxdata.csx

    Get-MailboxStatistics| FT DisplayName, TotalItemSize, TotalDeletedItemSizem, ItemCount

    Working with Command Output
    LVL 52

    Expert Comment

    So you can simply run the basic Exchange shell commands
    Get-MailboxStatistics| FT DisplayName, TotalItemSize, TotalDeletedItemSizem, ItemCount |Out-file MailboxStatistics.csv
    Get-Mailbox or others to get the data into CSV

    Once you have all the required data you can simply merge the CSV files into one by running the below command
    copy *.csv merged.csv

    - Rancy
    LVL 18

    Expert Comment

    PowerShell OutputHi,

    here cmdlet that I tried at my enviroment:

    $Users = Import-Csv C:\PS\LegacyExchange.csv
    $Output = foreach ($User in $Users ) {
    $mbxes = Get-MailboxStatistics -Identity $User.alias | Select-Object databasename,displayname,LastLogonTime,@{n='TotalItemsSize';e={
    If($_.TotalItemSize.value.ToBytes() -gt 1GB){
    }elseif ($_.TotalItemSize.value.ToBytes() -gt 1MB){
    }else {
    }},itemcount,@{n='OU';e={(Get-Mailbox -Identity $User.alias).OrganizationalUnit}}
    $folders = Get-MailboxFolderStatistics -Identity $User.alias | Select-Object name,@{n='FolderSize';e={
    If($_.FolderSize.ToBytes() -gt 1GB){
    }elseif ($_.FolderSize.ToBytes() -gt 1MB){
    }else {
    $mbx = New-Object PSObject
    $mbx | Add-Member -type noteproperty -Name TotalItemsSize -Value $Mbxes.TotalItemsSize
    $mbx | Add-Member -type noteproperty -Name Displayname -Value $Mbxes.Displayname
    $mbx | Add-Member -type noteproperty -Name Database -Value $Mbxes.Databasename
    $mbx | Add-Member -type noteproperty -Name ItemsCount -Value $Mbxes.ItemCount
    $mbx | Add-Member -type noteproperty -Name LastLogon -Value $Mbxes.LastLogonTime
    $mbx | Add-Member -type noteproperty -Name Organizational -Value $Mbxes.OU
    $mbx | Add-Member -type noteproperty -Name FolderName -Value $Folders[9].name
    $mbx | Add-Member -type noteproperty -Name FolderSize -Value $folders[9].FolderSize
    $mbx | Format-Table TotalItemsSize,Displayname,Database,Organizational,LastLogon,FolderName,ItemsCount,FolderSize
    $Output >C:\ps\result1.csv

    Open in new window

    The Output will be look like screenshot.

    My problem only for list of folders name/size. I not sure how if there is are more than 1 folders which meet the folder size creteria. Now, I only able to point for specific folder.

    Author Comment

    suriyaehnop thanks for your suggestion as I have tried this but like your example this creates a header for each iteration of a user. I think I need to drop the add-member command and find some other way of representing the data being displayed outward.

    For the others merging the contents of the csv file is a good choice but get-mailbox and get-mailboxstatistics pulls different data.  Get-mailboxstatistics pulls everything and  the system attendant and system mailboxes while the get-mailboxstatistics just pull user resource and shared mailboxes

    Author Comment

    Thank you for your efforts guys I will continue trying to make this work.

    Author Comment

    I finally got this to work. I learned that when combining two commands together it is easier to create an object to display them in column format. If I want to display them in list format it is better to use the add-member commandlet.

    Get-Mailbox  -ResultSize Unlimited | Foreach-Object{

        $mbx = $_ | Select Displayname, samaccountname, Organizationalunit, DataBase
        $stats = Get-MailboxStatistics $_ | Select Lastlogontime, @{n='Totalitemsize';e={
        If ($_.TotalItemSize.Value.ToBytes() -gt 1GB) {


          ElseIf ($_.TotalItemSize.Value.ToBytes() -gt 1MB) {

        }       Else {

        } }}, itemcount

        New-Object -TypeName PSObject -Property @{
            Displayname = $mbx.Displayname
          samaccountname = $mbx.samaccountname
            Organizationalunit = $mbx.Organizationalunit
            DataBase = $mbx.DataBase
            Lastlogontime = $stats.Lastlogontime
            TotalItemSize = $stats.TotalItemSize
          itemcount = $stats.itemcount

    } | Export-Csv c:\temp\test12.csv -NoTypeInformation
    LVL 67

    Accepted Solution

    I would say instead that Add-Member is better for dynamically added (optional, conditional) attributes, while New-Object is creating uniform objects. You should prefer New-Object (for basic attributes), and use Add-Member then for attributes you want to have only when a condition is met.

    However, the format-* cmdlets only look for the type of the first object in a pipeline, and output its attributes for all objects. This can be very misleading, as important attributes of other object types might not appear in the output.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Email statistics and Mailbox database quotas You might have an interest in attaining information such as mailbox details, mailbox statistics and mailbox database details from Exchange server. At that point, knowing how to retrieve this information …
    Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
    In this video we show how to create a Distribution Group in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >>…
    In this video we show how to create an Accepted Domain in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Ac…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now