Private/New-pseScheduledTaskAction.ps1

function New-pseScheduledTaskAction {

    [cmdletbinding()]

    param (
        [bool]$IsPs1Script,

        [string]$path,

        [string]$arguments,

        [string]$startin
    )
    try {
        Write-Host "Assigning Task Action settings" -ForegroundColor DarkGray

        #If it is a powershell script, then use the powershell.exe as the program and specify the arguments
        If ($IsPs1Script) {
            if ([string]::IsNullOrEmpty($arguments)) {
                $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$Path`""
            } else {
                $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$Path`" $arguments"
            }
        }

        # If it is not a powershell script, then check if arguments or StartIn were used
        if (!($IsPs1Script)) {
            $taskAction = New-ScheduledTaskAction -Execute $Path

            if (![string]::IsNullOrEmpty($Arguments)) {
                # $arguments not null or empty then...
                $taskAction.Arguments = $Arguments
                Write-Host "Task Action arguments are set to: $($taskaction.arguments)" -ForegroundColor DarkGray
            }
        }

        if (![string]::IsNullOrEmpty($StartIn)) {
            # $startin not null or empty then...
            $taskAction.WorkingDirectory = $StartIn
            Write-Host "Task Action workingdirectory is set to: $($taskaction.workingdirectory)" -ForegroundColor DarkGray
        }

        Write-Host "Task Action settings assigned successfully" -ForegroundColor DarkGreen

        return $taskAction
    } catch {
        Write-Error "Task Action settings could not be assigned: $x" -stop
    }
}