'PowerShell Core script runs but is not executed by Task Scheduler?

I am trying to schedule a PowerShell Core 7.2 script to run on Windows Server 2012R2. The script runs manually, without any errors, from the server and the Task Scheduler runs the task. In the History, I can see Task Completed

The issue is that the script is not executed. It is supposed to move the files and files are not moving which means the script was not executed.

Settings of the Task Scheduler that are selected are as follows:

General - Run whether user is logged on or not, Run with the highest privileges.
Actions -> Action Start a Program Actions -> Program/Script "C:\Program Files\PowerShell\7\pwsh.exe" (location of pwsh.exe)
Actions -> Add Arguments -Executionpolicy Bypass -File "R:\Public\IT\Vantage_Utilities\CNC_Scripts\File Transfer\Fastems\CNC_File_Transfer_Fastems.ps1"
Location -> Name of the local machine

I am not really sure what is going wrong here.

EDIT I am thinking there is an issue with the script. Because there is another script set up to be executed with PS Core and Task Scheduler. I am going to post the script here. It is a simple batch file that moves all the contents of one folder from one server to another. I achieve this in two functions. Function MoveFiles moves all the contents of the parent folder(excluding the subfolder called "Mazak"). The second function,Function MoveMazakFiles function moves the contents of "Mazak" only. ( I am completely aware I could have done this using fewer lines of code but that is not the point here)

Code:

$logPath = "\\MMS25163S1\Public\IT\Vantage_Utilities\CNC_Scripts\File Transfer\Fastems\Log.txt"
$trancriptPath = "\\MMS25163S1\Public\IT\Vantage_Utilities\CNC_Scripts\File Transfer\Fastems\LogTranscript.txt"
$getDate = Get-Date -Format "dddd MM/dd/yyyy HH:mm "
$counter = 0
$mazakCounter = 0
Start-Transcript -Path $trancriptPath -Append
Add-Content -Path $logPath -Value ("LOG CREATED $getDate") -PassThru
#Sources 
$srcMca = "\\MMS25163S1\Public\NcLib\FromNC\*"
$srcMcaNameChg ="\\MMS25163S1\Public\NcLib\FromNC"
$srcMazak= "\\MMS25163S1\Public\NcLib\FromNC\Mazak\*"
$srcMcaNameChgMazak = "\\MMS25163S1\Public\NcLib\FromNC\Mazak"
#Destination 
$destMca = "\\Sidney2\MfgLib\RevisedPrograms\MC-A"
#Time with milliseconds
$time = (Get-Date -Format hh-mm-fff-tt).ToString() 

Function MoveFiles{
    Param(
        [string]$src,
        [string]$dest,
        [string]$srcNameChange
    )
   Get-Item -Path $src -Exclude *Mazak* -ErrorAction SilentlyContinue | ForEach-Object{
        $counter++
        $fileName = $_.BaseName
        $fileNameExt = $_.Name
        Write-host $fileName -ForegroundColor Green
        Rename-Item -Path "$srcMcaNameChg\$fileNameExt"  -NewName ($fileName+"_"+"(Time-$time)"+$_.Extension);
        Add-Content -Path $logPath -Value ("Name changed: Time stamp added to $fileName ") -PassThru
    }
    Move-Item -Path $src -Exclude *Mazak*  -Destination $dest -Force
   Add-Content -Path $logPath -Value ("$counter file(s) moved to $dest") -PassThru
} 
MoveFiles -src $srcMca -dest $destMca -srcNameChange $srcMcaNameChg

Function MoveMazakFiles{
    Param(
        [string]$srcMazak,
        [string]$dest,
        [string]$srcNameChange
    )
    Get-ChildItem $srcMazak -Recurse -ErrorAction SilentlyContinue | ForEach-Object{
        $mazakCounter++
        $fileName = $_.BaseName
        $fileNameExt = $_.Name
        Write-host $fileName -ForegroundColor Green
        Rename-Item -Path "$srcMcaNameChgMazak\$fileNameExt"  -NewName ($fileName+"_"+"(Time-$time)"+$_.Extension);  
    }
    Move-Item -Path $srcMazak  -Destination $dest -Force
    Add-Content -Path $logPath -Value ("$mazakCounter file(s) from Mazak folder moved to $dest") -PassThru
}
MoveMazakFiles -srcMazak $srcMazak -dest $destMca -srcNameChange $srcMcaNameChg

Stop-Transcript


Solution 1:[1]

When setting the scheduled task, under Action -> Start a Program -> Program/Script

input this

"C:\Program Files\PowerShell\7\pwsh.exe" -File "R:\Public\IT\Vantage_Utilities\CNC_Scripts\File Transfer\Fastems\CNC_File_Transfer_Fastems.ps1"

The scheduler may ask if you wanting to run your powershell executable with the params -File "R:\Path to script\CNC_File_Transfer_Fastems.ps1" click yes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 swani14