title: No email and housekeep when add remove-item
body: <p>Scripting is about if not able find path, then will send email and output to FileA. If able to find path and delete any log older than 30 days and output to FileA. If add Get-ChildItem that row, the script below will not do any housekeep + output to txt file and send email to user. If i revoke the Get-ChildItem that row, it will send email to user.</p>
  <p>What I want:</p>
  <ol>
  <li>Send email if cannot find path and output to FileA. If able to find path, housekeep log older than 30 days and output to FileA</li>
  <li>Send email if cannot find path and output to FileB. If able to find path, housekeep log older than 30 days and output to FileB</li>
  </ol>
  <p>####Declare file path to be housekeep
  $housekeepLog = &quot;D:\logs-Copy*&quot;       &lt;&lt; valid path
  $housekeepLog2 = &quot;D:\logs-Copy2*&quot;     &lt;&lt; not valid path, purposely to test send email</p>
  <pre><code>####Housekeep any log older than 30days
  &gt;when add 2 sentence below, cannot send out email and no output to text file. when remove this, will send email and output to text file.
  Get-ChildItem –Path $housekeepLog -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item 
  Get-ChildItem –Path $housekeepLog2 -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item
  
  
  ####Output file
  $outputHousekeep = &quot;D:\Housekeep.txt&quot;
  $outputHousekeep2 = &quot;D:\Housekeep2.txt&quot;
  
  $timestampDateTime = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
  
  
  #### housekeepLog
  $EmailSplatHousekeep = @{
  To =  &quot;myemail@mydomain.com&quot;
  CC =  &quot;myemail@mydomain.com&quot;
  SmtpServer = &quot;smptserver@mydomain.com&quot; 
  From = &quot;noreply@mydomain.com&quot;
  Priority = &quot;High&quot;
  BodyAsHtml = $true
  Subject = &quot;Folder of Log File Does't Exist&quot;
  }
  
  if (!(Test-Path $housekeepLog))
  {
      $EmailSplatHousekeep.body = 
      &quot;
      1. do something
      &quot;
      
      Send-MailMessage @EmailSplatHousekeep
      Write-Output &quot;$timestampDateTime The folder $housekeepLog doesn't exist! Check the folder path!&quot; | Out-file $outputHousekeep  -Append
  }
  else
  {
      ####Input to outputfile
      Write-Output &quot;$timestampDateTime Done housekeeping in $housekeepLog .&quot; | Out-file $outputHousekeep  -Append
  }
  
  #### housekeepLog2
  $EmailSplatHousekeep2 = @{
  To =  &quot;myemail@mydomain.com&quot;
  CC =  &quot;myemail@mydomain.com&quot;
  SmtpServer = &quot;smptserver@mydomain.com&quot; 
  From = &quot;noreply@mydomain.com&quot;
  Priority = &quot;High&quot;
  BodyAsHtml = $true
  Subject = &quot;Folder of Log File Does't Exist&quot;
  }
  
  if (!(Test-Path $housekeepLog2))
  {
      $EmailSplatHousekeep2.body = 
      &quot;
      1. do something
      &quot;
      
      Send-MailMessage @EmailSplatHousekeep2
      Write-Output &quot;$timestampDateTime The folder $housekeepLog2 doesn't exist! Check the folder path!&quot; | Out-file $outputHousekeep2  -Append
  }
  else
  {
      ####Input to outputfile
      Write-Output &quot;$timestampDateTime Done housekeeping in $housekeepLog2 .&quot; | Out-file $outputHousekeep2  -Append
  }
  </code></pre>