How to setup sfc /scannow to run automaticly

Piotr Dzierżanowski 0 Reputation points
2024-09-17T08:00:55.77+00:00

I'd like to run sfc /scannow command every day and receive email if errors occurred.

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,054 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Wesley Li 8,780 Reputation points
    2024-09-17T14:46:09.8433333+00:00

    Hello

    To set up the sfc /scannow command to run automatically every day and receive an email if errors occur, you can use Task Scheduler and a PowerShell script. Here’s a step-by-step guide:

    Create a PowerShell Script:

    Open Notepad and paste the following script:

    $logFile = "C:\sfcLog.txt"

    sfc /scannow | Out-File -FilePath $logFile

    $errors = Select-String -Path $logFile -Pattern "Windows Resource Protection found corrupt files"

    if ($errors) {

        Send-MailMessage -From `your-email@example.com`-To`your-email@example.com` -Subject "SFC Scan Errors" -Body "Errors were found during the SFC scan. Please check the log file at $logFile." -SmtpServer "smtp.example.com"

    }

    Save the file with a .ps1 extension, for example, sfcScan.ps1.

    Set Up Task Scheduler:

    Open Task Scheduler and click on "Create Basic Task".

    Name the task (e.g., "Daily SFC Scan") and click "Next".

    Choose "Daily" and set the time you want the task to run.

    Select "Start a Program" and click "Next".

    In the "Program/script" field, type powershell.exe.

    In the "Add arguments" field, type -File "C:\path\to\sfcScan.ps1".

    Click "Next" and then "Finish".

    Configure Email Settings:

    Ensure that your PowerShell script has the correct SMTP server settings and email addresses.

    You might need to adjust your email server settings to allow sending emails via PowerShell.

    This setup will run the sfc /scannow command daily and send you an email if any errors are found during the scan.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.