Powershell ile bir klasor içerisindeki dizinleri ayrı ayrı olarak boyutlarını aşağıda ki script yardımı ile listeyebilirsiniz.

Aşağıda ki kodu bir dosyaya kayıt edin ve powershell’e .\dosyaadı -Path Dizinadı yazarak çalıştırın.

## Bu script powershell 2.0 ile uyumludur.

You can calculate subfolders size with below powershell script.

Copy below powershell codes and run script file. you can use this command for calculate ; .\Script-File-Name -Path FolderName

## This script is compatible with Powershell 2.0.

#>
[cmdletbinding()]
param(
[Parameter(Mandatory=$false)]
[Alias('Path')]
[String[]]
$basePath = 'C:\users',

[Parameter(Mandatory=$false)]
[Alias('Name')]
[String[]]
$folderName = 'all'
)

function Get-FolderSize {
[cmdletbinding()]
param(
[Parameter(Mandatory=$false)]
[Alias('Path')]
[String[]]
$basePath = 'C:\users',

[Parameter(Mandatory=$false)]
[Alias('User')]
[String[]]
$folderName = 'all'
)

#Get a list of all the directories in the base path we're looking for.
if ($folderName -eq 'all') {

$allFolders = Get-ChildItem -Path $basepath | Where-Object { $_.PSIsContainer }

} else {

$allFolders = Get-ChildItem -Path $basepath | Where-Object { $_.PSIsContainer }| Where-Object {$_.BaseName -like $folderName}

}

#Create array to store folder objects found with size info.
[System.Collections.ArrayList]$folderList = @()

#Go through each folder in the base path.
ForEach ($folder in $allFolders) {

#Clear our the variables used in the loop.
$fullPath = $null
$folderProp = $null
$folderObject = $null
$folderSize = $null
$folderSizeInMB = $null
$folderSizeInGB = $null
$folderBaseName = $null

#Set the fullPath, profile name, and folderinfo so we can work with those objects later.
$fullPath = $folder.FullName
$folderBaseName = $folder.BaseName

#Catch any errors when attempting to access the folders.
Try {

$folderInfo = Get-Childitem -Path $fullPath -Recurse -ErrorAction Stop

}

Catch {

Write-Host "Error getting info for [$folderName]. Message [$($_.Exception.Message)]" -foregroundColor Red -backGroundColor DarkBlue

}

#Use a try/catch so we can catch which folders are empty without a nasty error popping up (due to Measure-Object not having something to measure).
Try {

#Set folderSize to use the folderInfo as declared above and then Measure-Object to calculate the size.
$folderSize = $folderInfo | Measure-Object -Property Length -Sum -ErrorAction Stop

}

Catch {

if (($_.ErrorDetails.Message) -eq 'The property "Length" cannot be found in the input for any objects.') {

#If the error above is encountered, we set the folder size to "Empty Folder"
$folderSize = 'Empty Folder'

}

}

#Empty folder size handling.
if ($folderSize -ne 'Empty Folder') {

#We use the string format operator here to show only 2 decimals, and do some PS Math.
$folderSizeInMB = "{0:N2} MB" -f ($folderSize.Sum / 1MB)
$folderSizeInGB = "{0:N2} GB" -f ($folderSize.Sum / 1GB)

} else {

#If they're empty, we'll just show empty.
$folderSizeInMB = "Empty"
$folderSizeInGB = "Empty"

}

#Here we create the properties we'll add to the object below.
$folderProp = @{FolderName = $folderBaseName}
$folderProp += @{'Size(MB)' = $folderSizeInMB}
$folderProp += @{'Size(GB)' = $folderSizeInGB}

#This is the object we're creating to add to the array $folderList.
$folderObject = New-Object psobject -Property $folderProp

#Add the object to the array
$folderList.Add($folderObject) | Out-Null

}

#Return the object array with the objects selected in the order specified.
Return $folderList | Select-Object FolderName,'Size(GB)','Size(MB)'

}

#Call the function with the parameters and arguments passed to the script.
Get-FolderSize -basePath $basePath -folderName $folderName

Yorumlayın