Search code examples
powershellpowershell-2.0powershell-3.0

Get PST file from every user and sort them into size


Hey Guys I have a task to do but I don't have any plan how to execute it. There are users in c:\users and I have to get all .pst files in der directory and sum them together. And at the end have to sort them in a table, so that we can see who uses the most disk space for .pst files


Solution

  • Have you tried to "at least" attempt to write something?

    Anyways, you can start with something along these lines:

    
    $path = Get-ChildItem -Path C:\users -Filter "*.pst" -Recurse | Select-Object -ExpandProperty Fullname
    
    For($i=0; $i -lt $path.Count; $i++){
    [pscustomobject] @{
        PSTsFound = $path[$i]
        }
    }
    

    With some help from Pat Richard: (UC Unleashed)

    function Get-PstFiles {
    
      [CmdletBinding(SupportsShouldProcess = $True)]
      param(
            [Parameter(Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, Mandatory = $False)]
            [ValidateNotNullOrEmpty()]
            [string]$path,
            [Parameter(Position = 1, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, Mandatory = $False)]
            [ValidateNotNullOrEmpty()]
            [string]$filter = "*.pst",
            [Parameter(Position = 2, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, Mandatory = $False)]
            [ValidatePattern(".csv")]
            [string]$file
      )
      Begin{
          $PSTFiles = @()
      }
      Process{
            Get-ChildItem $path -recurse -Filter $filter | ? {$_.PSIsContainer -eq $False} | % {
                $obj = New-Object PSObject
                $obj | Add-Member NoteProperty Directory $_.DirectoryName
                $obj | Add-Member NoteProperty Name $_.Name
                $obj | Add-Member NoteProperty "Size in MB" ([System.Math]::Round(($_.Length/1mb),2))
                $obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
                $PSTFiles += $obj
            }
      }
      end{
            if ($file){
                $PSTFiles | Export-CSV "$file" -NoTypeInformation 
            }else{
                $PSTFiles
            }
      }
    }
    

    With the Syntax being like so: Get-PstFiles [[-path] ] [[-filter] ] [[-file] ] [-WhatIf] [-Confirm] []

    Example: Get-PstFiles -path C:\Users