How to Use PowerShell to Efficiently Locate Oversized OST Files

In today’s digitized workplace, the effective management of storage spaces on computer systems is paramount. As we rely more on digital communication, one tool that many professionals rely on is Microsoft Outlook. With prolonged use, the OST (Outlook Offline Storage Table) files can grow significantly in size, potentially occupying vast amounts of disk space. Here, we introduce a PowerShell script that aids IT professionals in locating oversized OST files

Background

The OST file is essentially a copy of mailbox items from Exchange Server. Over time, as more data accrues, these files can burgeon, posing storage challenges. Especially for IT professionals and Managed Service Providers (MSPs), keeping tabs on such large files is essential to ensure optimal system performance and to manage storage efficiently. It’s not just about freeing up space; it’s about understanding user behavior, optimizing resources, and preempting potential issues.

The Script

<#
.SYNOPSIS
    Find large OST files in the user's folder or recursively under C:.
.DESCRIPTION
    Find large OST files in the user's folder or recursively under C:.
.PARAMETER MinSize
    The minimum file size. This expects the file size to be in gigabytes.
.PARAMETER AllFolders
    Will search all folders under C:.
.EXAMPLE
     -MinSize 50
    Search for OST files larger than 50GB in each user's Outlook folder.
.EXAMPLE
     -AllFolders -MinSize 50
    Search for OST files larger than 50GB under C: recursively.
.OUTPUTS
    String[]
.NOTES
    Minimum OS Architecture Supported: Windows 10, Windows Server 2016
    Exit code 1: If at least 1 OST was found larger than MinSize
    Exit code 0: If no OST's where found larger than MinSize
    Release Notes:
    Initial Release
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
    Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>
[CmdletBinding()]
param (
    [Parameter()]
    [double]
    $MinSize = 50,
    [switch]
    $AllFolders
)

begin {
    function Test-IsElevated {
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $p = New-Object System.Security.Principal.WindowsPrincipal($id)
        $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }
}
process {
    if (-not (Test-IsElevated)) {
        Write-Error -Message "Access Denied. Please run with Administrator privileges."
        exit 1
    }
    $script:Found = $false

    if ($AllFolders) {
        $FoundFiles = Get-ChildItem C: -Filter *.ost -Recurse -Force -ErrorAction SilentlyContinue | Where-Object { $_.Length / ($MinSize * 1GB) -gt 1 }
        $FoundFiles | Select-Object FullName, Length | ForEach-Object {
            $Name = $_.FullName
            $Size = $_.Length
            Write-Host "$Name $Size bytes"
        }
        # If you wish to automatically remove the file(s) uncomment the line below. Do note that this is permanent! Make backups!
        # $FoundFiles | Remove-Item -Force -Confirm:$false
        if ($FoundFiles) {
            $script:Found = $true
        }
    }
    else {
        $UsersFolder = "C:Users"
        $Outlook = "AppDataLocalMicrosoftOutlook"
        Get-ChildItem -Path $UsersFolder | ForEach-Object {
            $User = $_
            $Folder = "$UsersFolder$User$Outlook"
            if ($(Test-Path -Path $Folder)) {
                $FoundFiles = Get-ChildItem $Folder -Filter *.ost | Where-Object { $_.Length / ($MinSize * 1GB) -gt 1 }
                $FoundFiles | Select-Object FullName, Length | ForEach-Object {
                    $Name = $_.FullName
                    $Size = $_.Length
                    Write-Host "$Name $Size bytes"
                }
                # If you wish to automatically remove the file(s) uncomment the line below. Do note that this is permanent! Make backups!
                # $FoundFiles | Remove-Item -Force -Confirm:$false
                if ($FoundFiles) {
                    Write-Verbose "Found"
                    $script:Found = $true
                }
            }
        }
    }

    if ($script:Found) {
        exit 1
    }
    exit 0
}
end {}

 

Access 300+ scripts in the NinjaOne Dojo

Get Access

Detailed Breakdown

The script can be compartmentalized into several key areas:

  • Synopsis and Description: Gives an overview of the script’s purpose, which is to find large OST files either in a user’s folder or recursively under C:.
  • Parameters: Determines the specifics of the search. This includes the minimum file size (in GB) and whether to search all folders under C:.
  • Elevation Check: Before delving into the file search, the script checks for administrative privileges. Without such rights, the script can’t probe deeper system levels.
  • File Search Logic: Depending on the chosen parameters, the script either looks in all folders under C: for OST files larger than the specified size or narrows the search to each user’s Outlook folder.
  • Output: If oversized OST files are found, the script prints their full path and size in bytes. Additionally, the script has commented-out lines for deleting these files – this action is left to the discretion of the IT professional.

Potential Use Cases

Imagine an IT administrator, Jane, at a sizable company. She receives complaints from various departments about system lag and decreased performance. After some diagnostic tests, Jane identifies storage issues – numerous large files occupying essential space. With this script, Jane can quickly pinpoint oversized OST files and determine the best course of action, whether it’s archiving, deleting, or relocating the files.

Comparisons

While there are GUI-based tools and software that provide disk analytics, the advantage of this PowerShell script is its specificity and automation potential. It directly targets OST files and can be incorporated into larger automation workflows, unlike many third-party tools that provide broader overviews without the same level of customizability.

FAQs

  • Can this script delete the located OST files?
    While the script identifies and lists large OST files, there’s a commented-out line for deletion. If uncommented, the script will remove the identified files.
  • Is the size parameter only in GB?
    Yes, the script expects the file size parameter in gigabytes.

Implications

Locating and managing large OST files is more than a storage exercise. It touches on IT security. Oversized OST files could be indicative of data hoarding or even data breaches, where vast amounts of data are exported. Proactively managing these files can mitigate potential risks.

Recommendations

  • Always back up data before running scripts that may potentially delete files.
  • Routinely run such scripts to maintain optimal system performance.
  • Do not uncomment the delete option unless you’re sure about the ramifications.

Final Thoughts

Managing large files, especially OST files, can be a daunting task. But tools like NinjaOne provide robust solutions for overall IT management. Coupled with tailored scripts like the one detailed here, professionals can ensure system efficiency, storage optimization, and risk minimization in the ever-evolving IT landscape.

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service deliver tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about NinjaOne Remote Script Deployment, check out a live tour, or start your free trial of the NinjaOne platform.

Categories:

You might also like

×

See NinjaOne in action!

By submitting this form, I accept NinjaOne's privacy policy.

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).