1
I have two simple one line "scripts". They check how old are certain files in a certain folders. One displays the output to the screen and the other writes the output to a text file on the desktop. Both ".ps1" script files are saved on the desktop. Running either ".ps1" files using "Run with PowerShell" gets results that are not correct. When running either ".ps1" files via PowerShell ISE the result is different (and correct).
The first script:
cd G:\moveh\back
Get-ChildItem -filter "*.rar" | Foreach-Object { if ( ((get-date).toshortdatestring() ) -gt ($_.lastwritetime.toshortdatestring() ) ) { "{0} {1}" -f $_.name,$_.lastwritetime.toshortdatestring()} }
cmd /c pause
The second script:
Get-ChildItem $allfolders | where {$_.Name -notlike "*closed"} | gci - Recurse -File -filter "Data.rar" | where {( ($_.lastwritetime.toshortdatestring()) -lt ((get-date).AddDays(-1).ToShortDateString() ) ) } | format-table -autosize -Property Directory, Name, LastWriteTime > C:\Users\itsupport\Desktop\Files_2b_updated_DB.txt
An example screenshot with the two different results for the first script:
Any ideas why there are different results?
I have two follow up questions: 1)Even though the strings are presented differently in ISE and in the PowerShell console the comparison is apples to apples so the results should be the same. In screenshot above, in ISE the output displays file names that are not displayed in the PowerShell console and vice versa. Why is that? – eliko – 2015-05-13T12:49:41.763
2.This is my new line:
– eliko – 2015-05-13T13:12:15.817Get-ChildItem -filter "*.rar" | Foreach-Object { if ( (get-date $_.lastwritetime -Format d) -lt (Get-Date -Format d )) { "{0} {1}" -f $_.name, (get-date $_.lastwritetime -Format d)} }
But the comparison is not working. I created a test folder with 10 files. The result of the comparison does not include files that are older than a day. Please see example screenshot@eliko See updated answer. – beatcracker – 2015-05-13T15:00:20.403