• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 553
  • Last Modified:

Need help displaying data in columns rather than lists powershell v2

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) {
      "$($_.TotalItemSize.Value.ToGB())GB"
    } ElseIf ($_.TotalItemSize.Value.ToBytes() -gt 1MB) {
      "$($_.TotalItemSize.Value.ToMB())MB"
    } Else {
      "$($_.TotalItemSize.Value.ToKB())KB"
    } }}, ItemCount
        $fldstats = Get-Mailboxfolderstatistics -id $_ |
    select-object name, @{n='Foldersize';e={
    If ($_.Foldersize.ToBytes() -gt 1GB) {
      "$($_.Foldersize.ToGB())GB"
    } ElseIf ($_.Foldersize.ToBytes() -gt 1MB) {
      "$($_.Foldersize.ToMB())MB"
    } Else {
      "$($_.Foldersize.ToKB())KB"
    } }},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


}
0
techdrive
Asked:
techdrive
1 Solution
 
Casey WeaverNetwork EngineerCommented:
I would use Format-Table for this. Microsoft has a nice explanation of the formatting options available in PS outputs.

http://technet.microsoft.com/en-us/library/dd347677.aspx

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
0
 
vicky19Commented:
You can always try

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

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

Working with Command Output
http://technet.microsoft.com/en-us/library/bb123533.aspx
0
 
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
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
0
A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

 
suriyaehnopCommented:
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){
"$($_.TotalItemSize.value.ToMB())GB"
}elseif ($_.TotalItemSize.value.ToBytes() -gt 1MB){
"$($_.TotalItemSize.value.ToMB())MB"
}else {
"$($_.TotalItemSize.value.ToKB())KB"
}

}},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){
"$($_.FolderSize.ToGB())GB"
}elseif ($_.FolderSize.ToBytes() -gt 1MB){
"$($_.FolderSize.ToMB())MB"
}else {
"$($_.FolderSize.ToKB())KB"
}


}}

$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.
0
 
techdriveAuthor Commented:
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
0
 
techdriveAuthor Commented:
Thank you for your efforts guys I will continue trying to make this work.
0
 
techdriveAuthor Commented:
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) {

      "$($_.TotalItemSize.Value.ToGB())GB"
          }

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

      "$($_.TotalItemSize.Value.ToMB())MB"
    }       Else {

      "$($_.TotalItemSize.Value.ToKB())KB"
    } }}, 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
0
 
QlemoC++ DeveloperCommented:
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.
0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now