119 lines
4.1 KiB
PowerShell
119 lines
4.1 KiB
PowerShell
# Script Name: Get-FileInventory.ps1
|
|
# Beschreibung: Erstellt eine Dateiliste mit Groesse, Erstellungsdatum und letzter Aenderung.
|
|
# Autor: Patrick Asmus
|
|
# Web: https://www.patrick-asmus.de
|
|
# Git-Reposit.: https://git.techniverse.net/scriptos/get-fileinventory.git
|
|
# Version: 1.0
|
|
# Datum: 16.06.2026
|
|
# Modifikation: Initial
|
|
#####################################################
|
|
|
|
param(
|
|
[string]$Path,
|
|
[string]$OutputDirectory = (Join-Path -Path $PSScriptRoot -ChildPath "exports"),
|
|
[switch]$NoExcel
|
|
)
|
|
|
|
# Variablen:
|
|
$TargetFolder = "C:\Temp"
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
if (-not $PSBoundParameters.ContainsKey("Path") -or [string]::IsNullOrWhiteSpace($Path)) {
|
|
$Path = $TargetFolder
|
|
}
|
|
|
|
function Convert-BytesToReadableSize {
|
|
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[long]$Bytes
|
|
)
|
|
|
|
if ($Bytes -ge 1GB) {
|
|
return "{0:N2} GB" -f ($Bytes / 1GB)
|
|
}
|
|
|
|
if ($Bytes -ge 1MB) {
|
|
return "{0:N2} MB" -f ($Bytes / 1MB)
|
|
}
|
|
|
|
if ($Bytes -ge 1KB) {
|
|
return "{0:N2} KB" -f ($Bytes / 1KB)
|
|
}
|
|
|
|
return "$Bytes B"
|
|
}
|
|
|
|
if (-not (Test-Path -LiteralPath $Path -PathType Container)) {
|
|
throw "Der angegebene Ordner wurde nicht gefunden: $Path"
|
|
}
|
|
|
|
$resolvedPath = (Resolve-Path -LiteralPath $Path).Path
|
|
|
|
if (-not (Test-Path -LiteralPath $OutputDirectory -PathType Container)) {
|
|
New-Item -ItemType Directory -Path $OutputDirectory | Out-Null
|
|
}
|
|
|
|
$resolvedOutputDirectory = (Resolve-Path -LiteralPath $OutputDirectory).Path.TrimEnd("\")
|
|
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
|
|
$safeFolderName = (Split-Path -Path $resolvedPath -Leaf)
|
|
if ([string]::IsNullOrWhiteSpace($safeFolderName)) {
|
|
$safeFolderName = "Laufwerk"
|
|
}
|
|
|
|
$csvPath = Join-Path -Path $OutputDirectory -ChildPath "Dateiliste-$safeFolderName-$timestamp.csv"
|
|
$xlsxPath = Join-Path -Path $OutputDirectory -ChildPath "Dateiliste-$safeFolderName-$timestamp.xlsx"
|
|
|
|
Write-Host "Durchsuche Ordner inklusive Unterordner:" -ForegroundColor Cyan
|
|
Write-Host " $resolvedPath"
|
|
Write-Host ""
|
|
|
|
$files = @(Get-ChildItem -LiteralPath $Path -File -Recurse -Force -ErrorAction SilentlyContinue |
|
|
Where-Object {
|
|
$filePath = $_.FullName
|
|
-not (
|
|
$filePath.Equals($resolvedOutputDirectory, [System.StringComparison]::InvariantCultureIgnoreCase) -or
|
|
$filePath.StartsWith("$resolvedOutputDirectory\", [System.StringComparison]::InvariantCultureIgnoreCase)
|
|
)
|
|
} |
|
|
Sort-Object FullName |
|
|
Select-Object `
|
|
@{Name = "Dateiname"; Expression = { $_.Name } },
|
|
@{Name = "Ordner"; Expression = { $_.DirectoryName } },
|
|
@{Name = "VollstaendigerPfad"; Expression = { $_.FullName } },
|
|
@{Name = "Endung"; Expression = { $_.Extension } },
|
|
@{Name = "GroesseBytes"; Expression = { $_.Length } },
|
|
@{Name = "GroesseLesbar"; Expression = { Convert-BytesToReadableSize -Bytes $_.Length } },
|
|
@{Name = "ErstelltAm"; Expression = { $_.CreationTime } },
|
|
@{Name = "ZuletztGeaendertAm"; Expression = { $_.LastWriteTime } })
|
|
|
|
if ($files.Count -gt 0) {
|
|
$files | Format-Table Dateiname, GroesseLesbar, ErstelltAm, ZuletztGeaendertAm -AutoSize
|
|
}
|
|
else {
|
|
Write-Host "Keine Dateien gefunden." -ForegroundColor Yellow
|
|
}
|
|
|
|
$files | Export-Csv -Path $csvPath -Delimiter ";" -NoTypeInformation -Encoding UTF8
|
|
Write-Host ""
|
|
Write-Host "CSV exportiert nach:" -ForegroundColor Green
|
|
Write-Host " $csvPath"
|
|
|
|
if (-not $NoExcel) {
|
|
$importExcelModule = Get-Module -ListAvailable -Name ImportExcel | Select-Object -First 1
|
|
|
|
if ($null -ne $importExcelModule) {
|
|
$files | Export-Excel -Path $xlsxPath -WorksheetName "Dateiliste" -AutoSize -FreezeTopRow -BoldTopRow
|
|
Write-Host "Excel-Datei exportiert nach:" -ForegroundColor Green
|
|
Write-Host " $xlsxPath"
|
|
}
|
|
else {
|
|
Write-Host "Excel-XLSX wurde uebersprungen, weil das Modul 'ImportExcel' nicht installiert ist." -ForegroundColor Yellow
|
|
Write-Host "Die CSV-Datei kann direkt mit Excel geoeffnet werden."
|
|
}
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host ("Gefundene Dateien: {0}" -f $files.Count) -ForegroundColor Cyan
|