SOLVED

Restricting PowerShell folder search to a specific date range

Brass Contributor

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

}

}

4 Replies

@Fred_Elmendorf 

 

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!

Thank you for your reply! The new code is helpful, but I described the problem incorrectly. I don't need to restrict the output, I need to restrict the count. I only want to count files created in 2023. Any further assistance greatly appreciated!
best response confirmed by Fred_Elmendorf (Brass Contributor)
Solution

@Fred_Elmendorf 

 

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.

Your reply was most helpful! I can now provide the needed information for that task, and I'm slowly learning more about PowerShell. My next challenge will be to examine file names to identify possible erroneous future dates.

Thanks again!

1 best response

Accepted Solutions
best response confirmed by Fred_Elmendorf (Brass Contributor)
Solution

@Fred_Elmendorf 

 

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.

View solution in original post