Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Define the drive or folder to scan
- $rootPath = "C:\" # Adjust to your target drive or folder
- # Define the base path for output CSV files
- $baseOutputCsv = "C:\temp\fileDetails" # Base path and name for output files
- # Initialize the file counter and line counter
- $fileCounter = 1
- $lineCounter = 0
- $linesPerFile = 5000 # Define how many lines you want per file
- function New-OutputCsv {
- param (
- [string]$basePath,
- [ref]$counter
- )
- $fileName = "${basePath}_$($counter.Value).csv"
- "Path,Size (KB),LastAccessTime,LastWriteTime,CreationTime,Permissions" | Out-File $fileName
- $counter.Value++
- $global:lineCounter = 0 # Reset line counter for the new file
- return $fileName
- }
- function Add-Line {
- param (
- [string]$line,
- [string]$outputCsv
- )
- Add-Content -Path $outputCsv -Value $line
- $global:lineCounter++
- # Check if we need to split into a new file based on line count
- if ($global:lineCounter -ge $global:linesPerFile) {
- $global:outputCsv = New-OutputCsv -basePath $baseOutputCsv -counter ([ref]$fileCounter)
- }
- }
- function Get-PermissionsSummary {
- param (
- [System.Security.AccessControl.FileSystemSecurity]$Acl
- )
- $permissions = @()
- foreach ($access in $Acl.Access) {
- # Correct usage of variable inside a string
- $identity = $access.IdentityReference.Value
- $rights = $access.FileSystemRights
- $accessType = $access.AccessControlType
- $permissions += "${identity}: $rights ($accessType)"
- }
- return $permissions -join "; "
- }
- function Safe-EnumerateFiles {
- param (
- [System.IO.DirectoryInfo]$Directory
- )
- # Handle directories
- try {
- $Directory.EnumerateDirectories() | ForEach-Object {
- Safe-EnumerateFiles -Directory $_
- }
- } catch [System.UnauthorizedAccessException] {
- Write-Host "Access denied: $($_.Exception.Message)"
- }
- # Handle files
- try {
- $Directory.EnumerateFiles() | ForEach-Object {
- $file = $_
- $sizeInKb = "{0:N2}" -f ($file.Length / 1KB)
- try {
- $acl = Get-Acl -LiteralPath $file.FullName
- $permissions = Get-PermissionsSummary -Acl $acl
- } catch {
- $permissions = "Failed to retrieve permissions"
- }
- $line = "$($file.FullName),$sizeInKb,$($file.LastAccessTime),$($file.LastWriteTime),$($file.CreationTime),`"$permissions`""
- Add-Line -line $line -outputCsv $global:outputCsv
- }
- } catch [System.UnauthorizedAccessException] {
- Write-Host "Access denied: $($_.Exception.Message)"
- }
- }
- # Create the first output CSV file
- $outputCsv = New-OutputCsv -basePath $baseOutputCsv -counter ([ref]$fileCounter)
- # Start the enumeration
- Safe-EnumerateFiles -Directory (New-Object System.IO.DirectoryInfo $rootPath)
- Write-Host "File details export completed. Files start with $baseOutputCsv`_1.csv"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement