I am creating a simple script to copy files and directories with modified date gt Feb. 5, 2018. To test I am simply trying to get a list of files that match. The script is not identifying files I know have dates gt the date I have in my script.
What’s weird to me is this outcome is only for certain files.
Any ideas?
Example: Get-Item E:\webapps\xml\web.xml | Foreach {[datetime]::parse($_.LastWriteTime)}
Result = Tuesday, February 13, 2018 10:51:54 AM
My script does not return this file when I point to the directory containing this file.
BTW: Get-ChildItem has a parameter -File to list only files … no need to use an extra Where-Object filter for that.
… I almost forgot … you should use format cmdlets (like Format-Table) always as the last cmdlet in the pipeline … if you want to pipe the outpur to a file you should use Select-Object instead.
Take a look at the Powershell Gotchas in Free Resources. Format-Table stops the pipeline, so nothing is going to Out-File. Recommend using Export-CSV as you are returning an objects, but your code does work:
I get what folks are saying but it’s not working for some files. Below is a test I just ran and the output.
For testing purposes I created a new text file (test.txt) in the same directory so it would have a current time stamp.
Is the file hidden? If it is hidden, you’ll need to add the -Force param to get the file. If it’s not hidden, does the file show up if you do not filter on date?
File properties show it’s normal, not hidden. I have hundreds of files and about 8 sub directories in the same path.
When I query files in the directory it shows the same as the test.txt file I created. I am totally confused by this one.
LastWriteTime : 5/6/2020 11:19:53 AM
Length : 0
Name : test.txt
LastWriteTime : 2/13/2018 10:51:54 AM
Length : 42249
Name : web.xml
I just noticed something strange in my test output though. There are 3 blank lines below the returned results, like it’s indicating there was another result but it’s not displayed. Notice below test.txt the blank lines, doesn’t really show on the forum, but there are 3 blank lines in my console window.
web.xml lastwritetime
Tuesday, February 13, 2018 10:51:54 AM
list of files & lastwritetime
LastWriteTime : 5/6/2020 11:19:53 AM
Length : 0
Name : test.txt