Powershell Help

Jay_2

2[H]4U
Joined
Mar 20, 2006
Messages
3,583
I am using a powershell script to list files over x minutes old

get-childitem -Path "C:\test" -recurse | where-object {$_.LastWriteTime -le (get-date).AddMinutes(-1)}

What this does is return the following

Directory: C:\test


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 31/10/2012 02:44 0 1.txt

I understand that adding > output.txt will write this to a file

What I need is to write out to a file 1 if it finds any files and 0 if it return no files.

Does anyone know how to do this?
 
$files = get-childitem -Path "C:\test" -recurse | where-object {$_.LastWriteTime -le (get-date).AddMinutes(-1)}

if ($files) {$files |Out-File 1.txt} else {$files | Out-File 0.txt}

But in this case the 0.txt file will be emtpy if there are no files that meet the filter.
 
Code:
$files = get-childitem -Path "C:\test" -recurse | where-object {$_.LastWriteTime -gt (get-date).AddMinutes(-45)} | Measure-Object

if ($files.Count -gt 0){Write-Host "1"} else {Write-Host "0"}


I think that one does it
 
Back
Top