Hey Guys I have to get the Size of every users .pst
files in there directory. My problem is, I don't realy know how to make a Foreach
which summs all .pst
Files in a users directory. Don't laugh I know that the the foreach loop is completly wron xD
$arr = Get-ChildItem \\Server\users\z01 | Where-Object {$_.PSIsContainer} | Foreach-Object {$_.Name} #Gets names of all Folders in this Directory
$Pfad = \\Server\users\z01\
foreach ($arr in $Pfad){
$Sum = Get-ChildItem -Path .\ -Include *pst | measure Length -sum
}
$Sum
I'd suggest first getting an array of objects in which the pst files FullName and its'Length is kept. That way you can also export this info to a CSV file for further analysis.
Then use that array to calculate the total size:
$Pfad = '\\Server\users\z01'
# leave out -Recurse if you do NOT want to include files in subdirectories
$allPstFiles = Get-ChildItem -LiteralPath $Pfad -Filter '*.pst' -File -Recurse | Select-Object FullName, Length
# you can output this on screen or write it to disk as Csv file, see
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv
# next get the sum of all sizes
$totalSize = ($allPstFiles | Measure-Object -Property Length -Sum).Sum
$totalSize
As per your latest comment, I understand that you want to have the sum of all *.pst files per user
(==> folder name inside $Verzeichniss
), formatted in GB with only two digits after the decimal point.
In that case, try:
$Verzeichniss = '\\Server\users\z01'
$userFolders = Get-ChildItem -LiteralPath $Verzeichniss -Directory
$Gesamt = foreach ($dir in $userFolders) {
# take the username from the folder name
$userName = $dir.Name
# find all *.pst files for this user
Get-ChildItem -LiteralPath $dir.FullName -Filter '*.pst' -File -Recurse |
# group them by a common property, in this case they all have .pst extension
Group-Object {$_.Extension} |
Select-Object @{Name = 'Name'; Expression = { $userName }},
@{Name = 'TotalSizeInGB'; Expression = { '{0:F2}' -f (($_.Group | Measure-Object Length -Sum).Sum / 1Gb ) }}
}
$Gesamt | Sort-Object -Property TotalSizeInGB -Descending | Select-Object -First 30