Question

I am new to Powershell and have been using the following snippet of code to pull the site quota information from my farm

$GetStorage = @{Name="Storage"; Expression={"{0:N2} MB" -f ($_.Usage.Storage/1048576)}}
$GetQuota = @{Name="Quota"; Expression={"{0:N2} MB" -f ($_.Quota.StorageMaximumLevel/1048576)}}
$GetUsage = @{Name="Used"; Expression={"{0:P2}" -f ($_.Usage.Storage/$_.Quota.StorageMaximumLevel)}}
Get-SPSite | Select-object URL,$GetStorage, $GetQuota, $GetUSage | Sort-Object Storage | Format-Table -AutoSize

This produces output like this:

Url                                 Storage     Quota        Used   
---                                 -------     -----        ----   
https://intdomain.com/sites/digital 156.93 MB   5,120.00 MB  3.07 % 
https://intdomain.com/sites/brazil  340.42 MB   5,120.00 MB  6.65 % 
https://intdomain.com/sites/pos     51.90 MB    2,560.00 MB  2.03 % 
https://intdomain.com/sites/EMEA    549.05 MB   2,560.00 MB  21.45 %
https://intdomain.com/sites/APAC    6,918.31 MB 10,240.00 MB 67.56 %
https://intdomain.com/sites/pr      655.82 MB   2,560.00 MB  25.62 %
https://intdomain.com               74.04 MB    2,560.00 MB  2.89 % 

As you'll see from my code, I'm trying to sort by the storage consumed, and it's only sorting it via the first number displayed - hence I have 6GB then 600MB

I'd like to be able to sort by this the quota used (the % column) but I have the same issue. The numbers do not sort in the right way.

I believe that it's because the numbers being returned are "Hash Tables" and not Integers.

Could someone help me correct the output so that it's sorted correctly?

Was it helpful?

Solution

Yes, it has to do with the number formatting. You've made Storage, Quota, and Used all strings. The results you see are consistent with sorting strings of numerical text.

Instead of having the units as part of the results, just use the numbers and then format the results in the table with a formatting expression:

$GetStorage = @{Name="Storage"; Expression={$_.Usage.Storage/1048576}}
$FormatStorage = @{label="Storage";Expression={$_.Storage}; FormatString="{0:N2} MB"}
$GetQuota = @{Name="Quota"; Expression={$_.Quota.StorageMaximumLevel/1048576}}
$FormatQuota = @{label="Quota";Expression={$_.Quota}; FormatString="{0:N2} MB"}
$GetUsage = @{Name="Used"; Expression={$_.Usage.Storage/$_.Quota.StorageMaximumLevel}}
$FormatUsage = @{label="Used";Expression={$_.Used}; FormatString="{0:P2}"}
Get-SPSite -Limit All | Select-object URL,$GetStorage, $GetQuota, $GetUSage | Sort-Object Storage | Format-Table URL, $FormatStorage, $FormatQuota, $FormatUsage -AutoSize

I also added a -Limit All parameter to the Get-SPSite so you'll get all the site collections in the farm.

OTHER TIPS

This is the Powershell script that I wrote to retrieve quota and size information for Site Collections in SharePoint. Run it as a SharePoint administrator to ensure that you can retrieve all the data. $spQuotaInfo is properly sortable, filterable, etc. and works with Export-Csv.

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

function Get-SPQuotaTemplate($spSite)
{
    return $spSite.WebApplication.WebService.QuotaTemplates | ?{ $_.QuotaID -eq $spSite.Quota.QuotaID }
}

function Get-SPApplicableQuota($spSite)
{
    [Microsoft.SharePoint.Administration.SPQuota] $spSiteQuota = $spSite.Quota

    if ($spSite.Quota.QuotaID -ne 0)
    {
        $spSiteQuotaTemplate = Get-SPQuotaTemplate $spSite

        if ($spSiteQuotaTemplate -ne $null)
        {
            $spSiteQuota = [Microsoft.SharePoint.Administration.SPQuota] $spSiteQuotaTemplate
        }
    }

    return $spSiteQuota
}

function Get-SPQuotaInfo()
{
    $quotaIDColumn = @{name = 'QuotaID'; expression = { $_.Quota.QuotaID } }
    $quotaTemplateColumn = @{name = 'QuotaTemplate'; expression = { $quotaID = $_.Quota.QuotaID; if ($quotaID -eq 0) { "No Template" } else { ((Get-SPQuotaTemplate $_).Name, "Unknown ($($quotaID))" -ne $null)[0] } } }
    $storageMaximumLevelColumn = @{ name = 'StorageMaximumLevel'; expression = { (Get-SPApplicableQuota $_).StorageMaximumLevel } }
    $storageWarningLevelColumn = @{ name = 'StorageWarningLevel'; expression = { (Get-SPApplicableQuota $_).StorageWarningLevel } }
    $storageUsedColumn = @{ name = 'StorageUsed'; expression = { $_.Usage.Storage } }

    $spQuotaInfo = $null

    # Sites must be retrieved with elevated privileges in order to ensure consistent access to the usage data.
    [Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({
        [Microsoft.SharePoint.SPSite[]] $spSites = Get-SPSite -Limit ALL
        $spQuotaInfo = $spSites | Select Url, $quotaIDColumn, $quotaTemplateColumn, $storageMaximumLevelColumn, $StorageWarningLevelColumn, $storageUsedColumn

        # Passes the array of PSCustomObjects generated by the Select back out to the parent.
        Set-Variable -Scope 1 -Name spQuotaInfo -Value $spQuotaInfo
    })

    return $spQuotaInfo
}

function Format-QuotaInfoTable($spQuotaInfo)
{
    $quotaTemplateDisplay = @{name = 'Quota Template'; expression = { $_.QuotaTemplate } }
    $storageMaximumLevelDisplay = @{ name = 'Storage Maximum Level'; expression = { if ($_.StorageMaximumLevel -eq 0) { "No Limit" } else { "{0:N0} MB" -f $_.StorageMaximumLevel / 1048576.0 } }; align = 'right' }
    $storageWarningLevelDisplay = @{ name = 'Storage Warning Level'; expression = { if ($_.StorageWarningLevel -eq 0) { "No Limit" } else { "{0:N0} MB" -f ($_.StorageWarningLevel / 1048576.0) } }; align = 'right' }
    $storageUsedDisplay = @{ name = 'Storage Used'; expression = { ($_.StorageUsed / 1048576.0) }; align = 'right'; formatString = '{0:N3} MB' }
    $percentQuotaUsedDisplay = @{ name = 'Quota Used'; expression = { if ($_.StorageMaximumLevel -eq 0) { 0 } else { $_.StorageUsed / $_.StorageMaximumLevel } }; formatString = '{0:P}' }

    $spQuotaInfo | ft -AutoSize Url, $quotaTemplateDisplay, $storageMaximumLevelDisplay, $storageWarningLevelDisplay, $storageUsedDisplay, $percentQuotaUsedDisplay
}

$spQuotaInfo = Get-SPQuotaInfo | Sort StorageUsed -Descending
Format-QuotaInfoTable ($spQuotaInfo)
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top