New-ScheduledTaskAction creates the action portion of a scheduled task. Supply the PowerShell executable through Execute, then pass the script path and script parameters as one Argument string. section 3.2.2
Build the action in three practical steps:
- Set Execute to the PowerShell executable, not the .ps1 file. section 3.2.2
- Construct Argument with -File, the script path, and any script parameters as a single string. section 3.2.2
- Create the action with New-ScheduledTaskAction, then combine it with a trigger and Register-ScheduledTask. section 3.2.2
Daily log cleanup action
An action can launch PowerShell 7 to run Invoke-LogFileCleanup.ps1 with log, archive, prefix, and retention parameters. Build the argument string with single-quoted PowerShell strings containing double-quoted paths, then splat Execute and Argument into New-ScheduledTaskAction.
