Reputation: 414
I am trying to create a report of files that are one day old, I currently have a script to include filename and date:
get-childitem -Path \\UNC_PATH_TO_FILE
| where-object {$_.lastwritetime -gt (get-date).addDays(-1)}
| Foreach-Object { $_.Name, $_.CreationTime }
Previously I was exporting this to a file for each UNC path, however this has now grown and will result in 22 separate files to be read.
I want to consolidate this into a CSV file which can contain a coll for Server, Filename ($_.Name
) and Date ($_.CreationTime
).
This is outside of my Powershell realm and cannot find anything to really help me. Would someone be able to offer some assistance?
Upvotes: 1
Views: 4247
Reputation: 54911
You could try this:
$paths = "\\server1\unc\path", "\\server1\unc\path"
Get-ChildItem -Path $paths
| Where-Object {$_.lastwritetime -gt (Get-Date).addDays(-1)}
| Select-Object @{n="Server";e={([uri]$_.FullName).Host}},
@{n="Filename";e={$_.Name}},
@{n="Date";e={$_.CreationTime}},
@{n="FileSize(MB)";e={[Math]::Round($_.Length/1MB,3)}}
| Export-Csv test.csv -NoTypeInformation
It extracts the servername from the UNC path and renames the other properties with the names you specified.
Upvotes: 2
Reputation: 126862
Here's an example that creates a new object for each file:
Get-ChildItem -Path \\UNC_PATH_TO_FILE |
Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)} |
ForEach-Object {
New-Object PSObject -Property @{
Server = $_.FullName -replace '^\\\\([\w]+)\\.+$','$1'
Name = $_.Name
CreationTime = $_.CreationTime
}
} | Export-Csv -Path files.csv -NoTypeInformation
Upvotes: 1
Reputation: 2053
The following should work:
get-childitem -Path \\UNC_PATH_TO_FILE
| where { $_.lastwritetime -gt (get-date).AddDays(-1)}
| foreach-object { $env.COMPUTERNAME,$_.Name, $_.CreationTime}
| Export-Csv -Path foo.txt -NoTypeInformation
Upvotes: 0