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 Profile photo of Rob Simmers Rob Simmers 2 months, 2 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #45976
    Profile photo of Jeremy Smit
    Jeremy Smit
    Participant

    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
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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
    Profile photo of Jeremy Smit
    Jeremy Smit
    Participant

    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

    • This reply was modified 2 months, 2 weeks ago by Profile photo of Jeremy Smit Jeremy Smit.
    #46105
    Profile photo of Jeremy Smit
    Jeremy Smit
    Participant

    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
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.