update
This commit is contained in:
125
Exemples/Get-FolderSize.ps1
Normal file
125
Exemples/Get-FolderSize.ps1
Normal file
@ -0,0 +1,125 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
|
||||
Cette fonction renvoi le nombre de fichier et la taille contenu dans le dossier ainsi que le détail pour les sous-dossiers sur N niveaux.
|
||||
Le résultat est un tableau indiquant le nombre et la taille des fichiers directement dans le dossier ainsi que le cumul des sous dossiers.
|
||||
Il est possible de définir l'unité pour les tailles (B,Kb,Mb,Gb), le nombre de niveau et de filtrer sur certains types de fichier (*.txt)
|
||||
La fonction utilise Get-childitem et Measure-Object.
|
||||
Attention à la version de PowerShell.
|
||||
|
||||
This function returns the number of files and the size contained in the folder as well as the detail for the subfolders on N levels.
|
||||
The result is a table showing the number and size of files directly in the folder as well as the total of subfolders.
|
||||
It is possible to set the unit for sizes (B, Kb, Mb, Gb), the number of level and filter on certain file types (* .txt)
|
||||
The function uses Get-childitem and Measure-Object.
|
||||
Watch out for the PowerShell version.
|
||||
|
||||
|
||||
.DESCRIPTION
|
||||
Cette fonction renvoie un tableau avec :
|
||||
- chemin du dossier (Path)
|
||||
- nombre de fichiers dans le dossier (InFolderFiles)
|
||||
- tailles des fichiers dans le dossier (InFolderSize)
|
||||
- nombre de fichier dans le dossier et les sous dossiers (AllFiles)
|
||||
- tailles des fichiers du dossier et des sous dossiers (AllSize)
|
||||
|
||||
This function returns an array with:
|
||||
- Path of the folder (Path)
|
||||
- number of files in the folder (InFolderFiles)
|
||||
- file sizes in the folder (InFolderSize)
|
||||
- number of files in the folder and subfolders (AllFiles)
|
||||
- file sizes of the folder and subfolders (AllSize)
|
||||
|
||||
|
||||
.PARAMETER FolderPath
|
||||
|
||||
Chemin du dossier à analyser
|
||||
Folder path to analyze
|
||||
|
||||
.PARAMETER Level
|
||||
|
||||
Nombre de niveau de sous dossier.
|
||||
Number of subfolder level.
|
||||
|
||||
.PARAMETER unit
|
||||
|
||||
Unité pour les tailles de fichiers (B,Kb,Mb,Gb).Par défaut :Gb.
|
||||
Unit for file sizes
|
||||
|
||||
.PARAMETER Filter (B,Kb,Mb,Gb), default Gb.
|
||||
|
||||
Filtre les fichiers par type. Par exemple *.txt ne compte que les fichiers txt.
|
||||
Filters files by type. For example * .txt only counts txt files.
|
||||
|
||||
|
||||
.EXAMPLE
|
||||
|
||||
.\Get-FolderSize.ps1 -FolderPath d:\tools -level 2
|
||||
.EXAMPLE
|
||||
|
||||
.\Get-FolderSize.ps1 -FolderPath \\Server\Documents -level 2 -filter "*.doc*" -unit "kb"
|
||||
|
||||
.NOTES
|
||||
Author: Philippe BARTH
|
||||
Version: 1.0
|
||||
#>
|
||||
|
||||
# Déclaration des paramètres
|
||||
param([string]$FolderPath,[int]$level,[string]$unit="Mb",[string]$Filter="*.*")
|
||||
|
||||
if ($level -lt 1) { $level = 1 }
|
||||
|
||||
# Determine unit for folder size, default Gb
|
||||
Switch ($unit)
|
||||
{
|
||||
"Gb"
|
||||
{
|
||||
$div="1GB"
|
||||
}
|
||||
|
||||
"Mb"
|
||||
{
|
||||
$div="1MB"
|
||||
}
|
||||
|
||||
"Kb"
|
||||
{
|
||||
$div="1KB"
|
||||
}
|
||||
"b"
|
||||
{
|
||||
$div="1"
|
||||
}
|
||||
Default
|
||||
{
|
||||
$div="1GB"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#init result
|
||||
$result=@()
|
||||
# search subfolder
|
||||
$SubFolders = (Get-ChildItem -path $FolderPath -recurse -Directory -Depth $($level-1)).FullName
|
||||
#add parent folder in list
|
||||
$SubFolders+=$FolderPath
|
||||
# scan all folder
|
||||
foreach ($subfolder in $SubFolders)
|
||||
{
|
||||
#Search file in folder and subfolder
|
||||
$FolderSize = Get-ChildItem -path $SubFolder -filter $filter -File -recurse | Measure-Object -Sum Length
|
||||
#Search file only in folder
|
||||
$fileInFolder = Get-ChildItem -path $SubFolder -filter $filter -File | Measure-Object -Sum Length
|
||||
|
||||
#result
|
||||
$result+= New-Object -TypeName PSObject -Property @{
|
||||
Path = $subfolder
|
||||
AllFiles = $foldersize.count
|
||||
AllSize = $foldersize.sum/$div
|
||||
InFolderFiles = $fileInFolder.count
|
||||
InFolderSize = $fileInFolder.sum/$div
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $result | Sort-Object -Property path
|
Reference in New Issue
Block a user