By José C. Nieves Pérez
$folderPath = "**************" # Replace with the folder path you want to audit
$reportPath = "C:\Temp\AuditReport.csv" # Replace with the path where you want to save the report
$files = Get-ChildItem $folderPath -Recurse | Where-Object { $_.PSIsContainer -eq $false }
$reportContent = foreach ($file in $files) {
[PSCustomObject] @{
"Name" = $file.Name
"Size (KB)" = "{0:N2}" -f ($file.Length / 1KB)
"Date Created" = $file.CreationTime
"Date Modified" = $file.LastWriteTime
"Last Date Accessed" = $file.LastAccessTime
"Owner" = (Get-Acl $file.FullName).Owner
"Type" = $file.Extension
"Attributes" = $file.Attributes
}
}
$reportContent | Export-Csv -Path $reportPath -NoTypeInformation
Write-Output "Audit report saved to $reportPath"
It sets the $folderPath variable to the path of the folder you want to audit.
It sets the $reportPath variable to the path where you want to save the report as a CSV file.
It uses the Get-ChildItem cmdlet to get all files in the folder and its subfolders, and then filters out any folders from the results.
It loops through each file and creates a PSCustomObject that represents the file’s audit data, including columns for name, size, date created, date modified, last date accessed, owner, type, and attributes.
It appends each PSCustomObject to an array of audit report data.
It exports the array of PSCustomObjects to a CSV file using the Export-Csv cmdlet.
It outputs a message indicating where the report was saved.
Note that the size is now in KB instead of MB, and the attributes column will contain a comma-separated list of file attributes such as «ReadOnly, Hidden, Archive». You can customize the script by changing the $folderPath and $reportPath variables to your desired values.