So i have a PS script which check files older than 90 days. The problem is the output. I need a output which just says " The folder contains files older than 90 days" . Instead of this the script gives the mentioned output for every single file like a list. is there way to change it?
Code :
$a = Get-ChildItem "*insert path*"
foreach($x in $a)
{
$y = ((Get-Date) – $x.CreationTime).Days
if ($y -gt 90 -and $x.PsISContainer -ne $True)
{
write-host "Files older than 90 days exist"
}
}
Output i get : Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist Files older than 90 days exist
CodePudding user response:
You just need to break
your loop if the condition is $true
at least once. In addition, Get-ChildItem
has a -File
switch to search only for files, you don't need to check if .PsISContainer
is $false
.
$files = Get-ChildItem "enter path here" -File
$limit = [datetime]::Now.AddDays(-90)
foreach($file in $files) {
if ($file.CreationTime -lt $limit) {
'{0} contains files older than 90 days' -f $file.Directory
break
}
}
CodePudding user response:
I figured out a solution for this.
$Path = "C:\Users\skmohan\Documents\Srija\Js\*"
$count = 0
foreach($x in $Path)
{
$y = ((Get-Date) – $x.CreationTime).Days
if ($y -gt 90 -and $x.PsISContainer -ne $True)
{
$count
}
}
if($count -gt 0)
{
write-host "$count Files older than 90 days exist"
}
else
{
Write-Host "No files older than 90 days"
}