Search code examples
powershelldirectoryget-childitem

Accessing a given file by date on PowerShell


I wrote this in PowerShell: Ideally it should check $myDIRECTORY and get all the files that have been created during $myCreationTime, it works flawlessly, now I want to access the file, so I wrote a For-Each-Object{} to print the file:

    Get-ChildItem $myDIRECTORY  | Where-Object {
    $_.CreationTime.Date.ToString() -match $myCreationTime | ForEach-Object{
        Write-Host $_.ToString()
    }
} 

However I get the boolean Match value, so:

False
False
True
False

How can I get the True file? $_.BaseName isn't working and it's unsurprisingly, I understand it's a boolean, so I don't even know why I tried it!


Solution

  • In your foreach loop you get only the result of the match operation.

    Try it like that:

    Get-ChildItem $myDIRECTORY  | ForEach-Object {
        If ($_.CreationTime.Date.ToString() -match $myCreationTime) {
            $_.BaseName
        }
    }