list mailbox from biggest to smalles and put out in a csv file

Welcome Forums General PowerShell Q&A list mailbox from biggest to smalles and put out in a csv file

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
2 years, 6 months ago.

  • Author
    Posts
  • #45976

    Participant
    Points: 0
    Rank: Member

    Hi People,

    I have a script to get al usermailboxes in exchange online.

    But i tried serveral options to list everything in descending and tried to get the output in a csv file. but failed

    any one the solution for me ?

    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $admin -Authentication Basic -AllowRedirection
    $ImportCmd = Import-PSSession $Session
    
    $userToFind = Read-Host -Prompt "Enter user to find (leave blank for all)"
    
    $params = @{}
    if([string]::IsNullOrEmpty($userToFind) -eq $false)
    {$params = @{Identity = $userToFind}
    }
    
    
    
    $UserMailboxStats = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited @Params | Get-MailboxStatistics
    $UserMailboxStats | Add-Member -MemberType ScriptProperty -Name TotalItemSizeInBytes -Value {$this.TotalItemSize -replace "(.*\()|,| [a-z]*\)", ""}
    $UserMailboxStats | Select-Object DisplayName, TotalItemSizeInBytes,@{Name="TotalItemSize (GB)"; Expression={[math]::Round($_.TotalItemSizeInBytes/1GB,2)}}
  • #45981

    Participant
    Points: 428
    Helping Hand
    Rank: Contributor

    Should be this simple:

    #Sort the stats on Bytes
    $sortedUserMailboxStats = $UserMailboxStats | Sort-Object -Property TotalItemSizeInBytes -Descending
    #Export to CSV
    $sortedUserMailboxStats | Export-CSV C:\UserMailboxStats.csv -NoTypeInformation
    
  • #46055

    Participant
    Points: 0
    Rank: Member

    Yeah great,

    I tested it and it looked it worked but i got the following error message

    Export-CSV : Toegang tot het pad C:\UserMailboxStats.csv is geweigerd.
    At Q:\Scripts\Test\list mailboxs Exchange online.ps1:23 char:27
    + ... rMailboxStats | Export-CSV C:\UserMailboxStats.csv -NoTypeInformation
    +                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OpenError: (:) [Export-Csv], UnauthorizedAccessException
        + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand
    

    and it is not listing from bigget to smalles

  • #46105

    Participant
    Points: 0
    Rank: Member

    My script so far, (i'm very new with this, if someone has a tip to learn for scripting let me know)

    $Username = "username"
    $Password = ConvertTo-SecureString "password" -AsPlainText -Force
    $Admin = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
    
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $admin -Authentication Basic -AllowRedirection
    $ImportCmd = Import-PSSession $Session
    
    $userToFind = Read-Host -Prompt "Enter user to find (leave blank for all)"
    
    $params = @{}
    if([string]::IsNullOrEmpty($userToFind) -eq $false)
    {$params = @{Identity = $userToFind}
    }
    
    
    $UserMailboxStats = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited @Params | Get-MailboxStatistics
    $UserMailboxStats | Add-Member -MemberType ScriptProperty -Name TotalItemSizeInBytes -Value {$this.TotalItemSize -replace "(.*\()|,| [a-z]*\)", ""}
    $UserMailboxStats | Select-Object DisplayName, TotalItemSizeInBytes,@{Name="TotalItemSize (GB)"; Expression={[math]::Round($_.TotalItemSizeInBytes/1GB,2)}}
    
    #Sort the stats on Bytes
    $sortedUserMailboxStats = $UserMailboxStats | Sort-Object -Property TotalItemSizeInBytes -Descending
    #Export to CSV
    $sortedUserMailboxStats | Export-CSV C:\UserMailboxStats.csv -NoTypeInformation
    
  • #46111

    Participant
    Points: 428
    Helping Hand
    Rank: Contributor

    You just need to provide a CSV path that you have access to write to, you are getting access denied creating the CSV. Just change the bolded path to your desktop or somewhere you have permissions to write\create:

    $sortedUserMailboxStats | Export-CSV C:\UserMailboxStats.csv -NoTypeInformation

The topic ‘list mailbox from biggest to smalles and put out in a csv file’ is closed to new replies.