Dear All,

i have created a task to clear exchange logs once a month, however it does not works.

the steps i have followed from this link Clear Exchange 2013 / 2016 log files

My scripts is :

 Set-Executionpolicy -Scope CurrentUser RemoteSigned
$days=5
$IISLogPath="C:\inetpub\logs\LogFiles\"
$ExchangeLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
$ETLLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
$ETLLoggingPath2="C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\"
Function CleanLogfiles($TargetFolder)
{
  write-host -debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder

    if (Test-Path $TargetFolder) {
        $Now = Get-Date
        $LastWrite = $Now.AddDays(-$days)
    #   $Files = Get-ChildItem $TargetFolder -Include *.log,*.blg, *.etl -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
        $Files = Get-ChildItem $TargetFolder -Recurse | Where-Object {$_.Name -like "*.log" -or $_.Name -like "*.blg" -or $_.Name -like "*.etl"}  | where {$_.lastWriteTime -le "$lastwrite"} | Select-Object FullName  
        foreach ($File in $Files)
            {
               $FullFileName = $File.FullName  
               Write-Host "Deleting file $FullFileName" -ForegroundColor "yellow"; 
                Remove-Item $FullFileName -ErrorAction SilentlyContinue | out-null
            }
       }
Else {
    Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red"
    }
}
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2) 

when i run the script manually it shows the below error but it does runs and clear the logs.

Last script run time shows 0X1

OS is Server 2016 and Exchange is 2016

5 Spice ups

Check other scopes of execution policies (powershell as admin)

i am not sure i underdtand @jimittermueller

do you mean run the job wit high privileges? if yes this is already enabled

Get-ExecutionPolicy -List
Maybe execution policy is set via gpo?

Agree with the method mentioned above, you can use Get-ExecutionPolicy -List to view scope and use Set-ExecutionPolicy to configure execution policy.
I found an article about execution policy for your reference.

In addition, according to error messages, you can follow steps mentioned in the article .

Hey @julienangelo3890 ​, plus one for the previous comments about execution policy. Did you try that out yet? If so did it work for what you were looking for? Just let us know!

@jrgenmittermller ​ the policy is as next

Windows PowerShell
 Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine    RemoteSigned

@joyzhang6691 ​ i am already using the excution on the argument to run the job

-ExecutionPolicy Bypass -file "C:\scripts\CleanLogs(AccessDeniedFix).PS1"

@sarah-for-paubox ​ apologies i am not sure i can follow, what do you mean?

I was just curious if you had tried what was already suggested and if that worked for you?

Based on your test information, you have configured the policy RemoteSigned and scope CurrentUser.

And then I went back to the script, at the beginning it says Set-Executionpolicy RemoteSigned but you run Set-Executionpolicy -Scope CurrentUser RemoteSigned.

Is this the cause of the error?

@joz ​ below is my PW scripts.

Set-Executionpolicy -Scope CurrentUser RemoteSigned
$days=7
$IISLogPath="C:\inetpub\logs\LogFiles\"
$ExchangeLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\Logging\"
$ETLLoggingPath="C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\ETLTraces\"
$ETLLoggingPath2="C:\Program Files\Microsoft\Exchange Server\V15\Bin\Search\Ceres\Diagnostics\Logs\"
Function CleanLogfiles($TargetFolder)
{
  write-host -debug -ForegroundColor Yellow -BackgroundColor Cyan $TargetFolder

    if (Test-Path $TargetFolder) {
        $Now = Get-Date
        $LastWrite = $Now.AddDays(-$days)
    #   $Files = Get-ChildItem $TargetFolder -Include *.log,*.blg, *.etl -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
        $Files = Get-ChildItem $TargetFolder -Recurse | Where-Object {$_.Name -like "*.log" -or $_.Name -like "*.blg" -or $_.Name -like "*.etl"}  | where {$_.lastWriteTime -le "$lastwrite"} | Select-Object FullName  
        foreach ($File in $Files)
            {
               $FullFileName = $File.FullName  
               Write-Host "Deleting file $FullFileName" -ForegroundColor "yellow"; 
                Remove-Item $FullFileName -ErrorAction SilentlyContinue | out-null
            }
       }
Else {
    Write-Host "The folder $TargetFolder doesn't exist! Check the folder path!" -ForegroundColor "red"
    }
}
CleanLogfiles($IISLogPath)
CleanLogfiles($ExchangeLoggingPath)
CleanLogfiles($ETLLoggingPath)
CleanLogfiles($ETLLoggingPath2)

@sarah-for-paubox ​ yes i’ve followed the steps but the script is not running.

Compare the scripts you provided with and the scripts I found on the official website.
The only difference is that the screenshot shows:T2.png

And your script is Set-Executionpolicy -Scope CurrentUser RemoteSigned.

I want to determine if it is related to this issue.

@joyzhang_ the author in the forum advised to change the execution policy which I did actually both policies are not working.
I am curious if someone else hier is using the script with server 2016 and able to run the script.

I ran the scripts in my environment successfully. You can run the scripts directly using EMS on the server.

@joyZhang_ i never said i cannot run the script.i can run it manually using the powershell or EMS. however when i create a task to do so its not running during the windows tasks.

i am looking to get this automated so it will run like onces a week.

You can try to restart task schedule in service.

In addition,you also can check the steps in creating task. for example, powershell path in this machine, the clear logs scripts path in this machine. For more details: Exchange 2013/2016/2019 Logging - Clear out the Log files