Mar 08 2023 12:39 PM
The help I've received here has been invaluable, and as expected, once something good has been accomplished, there are always new requirements. So... Thanks in advance for continuing to assist this PowerShell novice.
How can I modify this code to restrict the results to only include LastWriteTimes in CY23?
$dirs = Get-ChildItem "\\Myorg\firstlevel\secondlevel\thirdlevel\Ion" -Directory
$csvLog = "\\Myorg\output\fileshare\mystuff\Documents\PowerShellOutput\Latest Ion Trend Files Size Counts.csv"
foreach ($dir in $dirs) {
$recentFile = $null
$folder = $dir.Name
$directory = $dir.FullName
$filesCount = (Get-ChildItem $directory -Filter "trend*.pqd" -Recurse).Count
$recentFile = Get-ChildItem $directory -Filter "trend*.pqd" -Recurse | Sort-Object LastWriteTime -Descending| Select-Object -First 1
$recentFileName = $recentFile.Name
$recentFileLength = $recentFile.Length
$recentFileWriteTime = $recentFile.LastWriteTime
if ($recentFile) {
$object = New-Object -TypeName psobject
$object | Add-Member -MemberType NoteProperty -Name "Site" -Value $folder
$object | Add-Member -MemberType NoteProperty -Name "File Name" -Value $recentFileName
$object | Add-Member -MemberType NoteProperty -Name "File Size" -Value $recentFileLength
$object | Add-Member -MemberType NoteProperty -Name "Date Time" -Value $recentFileWriteTime
$object | Add-Member -MemberType NoteProperty -Name "File Count" -Value $filesCount
$object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation
}
}
Mar 08 2023 09:37 PM
To filter the results based on the LastWriteTime of the files, you can add a Where-Object clause to the Get-ChildItem command that retrieves the files. You can use the -ge (greater than or equal to) and -lt (less than) operators to specify a range of dates.
Here's an updated version of your code that should filter the results based on the LastWriteTime being in CY23 (assuming that means the year 2023):
$dirs = Get-ChildItem "\\Myorg\firstlevel\secondlevel\thirdlevel\Ion" -Directory
$csvLog = "\\Myorg\output\fileshare\mystuff\Documents\PowerShellOutput\Latest Ion Trend Files Size Counts.csv"
$startDate = Get-Date "01/01/2023"
$endDate = Get-Date "01/01/2024"
foreach ($dir in $dirs) {
$recentFile = $null
$folder = $dir.Name
$directory = $dir.FullName
$filesCount = (Get-ChildItem $directory -Filter "trend*.pqd" -Recurse).Count
$recentFile = Get-ChildItem $directory -Filter "trend*.pqd" -Recurse `
| Where-Object { $_.LastWriteTime -ge $startDate -and $_.LastWriteTime -lt $endDate } `
| Sort-Object LastWriteTime -Descending `
| Select-Object -First 1
if ($recentFile) {
$recentFileName = $recentFile.Name
$recentFileLength = $recentFile.Length
$recentFileWriteTime = $recentFile.LastWriteTime
$object = New-Object -TypeName psobject
$object | Add-Member -MemberType NoteProperty -Name "Site" -Value $folder
$object | Add-Member -MemberType NoteProperty -Name "File Name" -Value $recentFileName
$object | Add-Member -MemberType NoteProperty -Name "File Size" -Value $recentFileLength
$object | Add-Member -MemberType NoteProperty -Name "Date Time" -Value $recentFileWriteTime
$object | Add-Member -MemberType NoteProperty -Name "File Count" -Value $filesCount
$object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation
}
}
This code defines a $startDate and $endDate variable to represent the range of dates you're interested in, then adds a Where-Object clause to the Get-ChildItem command that filters the results based on the LastWriteTime being between those dates. The rest of the code is the same as before.
Let me know if that helps!
Mar 09 2023 05:42 AM
Mar 09 2023 06:48 AM
Solution
No problem! You can modify the script to count only the files that were created in 2023. Here's how you can do it:
$dirs = Get-ChildItem "\\Myorg\firstlevel\secondlevel\thirdlevel\Ion" -Directory
$csvLog = "\\Myorg\output\fileshare\mystuff\Documents\PowerShellOutput\Latest Ion Trend Files Size Counts.csv"
$startDate = Get-Date "01/01/2023"
$endDate = Get-Date "01/01/2024"
foreach ($dir in $dirs) {
$folder = $dir.Name
$directory = $dir.FullName
$filesCount = (Get-ChildItem $directory -Filter "trend*.pqd" -Recurse `
| Where-Object { $_.CreationTime -ge $startDate -and $_.CreationTime -lt $endDate }).Count
$object = New-Object -TypeName psobject
$object | Add-Member -MemberType NoteProperty -Name "Site" -Value $folder
$object | Add-Member -MemberType NoteProperty -Name "File Count" -Value $filesCount
$object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation
}
In the modified script, we are still iterating through the directories, but instead of counting and outputting the latest file, we are counting the number of files that were created in 2023. We achieve this by filtering the files based on their CreationTime property and checking if it falls within the startDate and endDate range.
We then create a PowerShell object and add the Site name and the File Count for that directory, and then export it to a CSV file.
Let me know if this solves your problem.
Mar 09 2023 02:18 PM
Mar 09 2023 06:48 AM
Solution
No problem! You can modify the script to count only the files that were created in 2023. Here's how you can do it:
$dirs = Get-ChildItem "\\Myorg\firstlevel\secondlevel\thirdlevel\Ion" -Directory
$csvLog = "\\Myorg\output\fileshare\mystuff\Documents\PowerShellOutput\Latest Ion Trend Files Size Counts.csv"
$startDate = Get-Date "01/01/2023"
$endDate = Get-Date "01/01/2024"
foreach ($dir in $dirs) {
$folder = $dir.Name
$directory = $dir.FullName
$filesCount = (Get-ChildItem $directory -Filter "trend*.pqd" -Recurse `
| Where-Object { $_.CreationTime -ge $startDate -and $_.CreationTime -lt $endDate }).Count
$object = New-Object -TypeName psobject
$object | Add-Member -MemberType NoteProperty -Name "Site" -Value $folder
$object | Add-Member -MemberType NoteProperty -Name "File Count" -Value $filesCount
$object | Export-Csv $csvLog -Encoding ASCII -Append -NoTypeInformation
}
In the modified script, we are still iterating through the directories, but instead of counting and outputting the latest file, we are counting the number of files that were created in 2023. We achieve this by filtering the files based on their CreationTime property and checking if it falls within the startDate and endDate range.
We then create a PowerShell object and add the Site name and the File Count for that directory, and then export it to a CSV file.
Let me know if this solves your problem.