3

I'm creating a window task using powershell, everthing is ok, but i can't find how to add the Author name. Register-ScheduledTask as a parameter for description but not for Author.

Exported Windows task

 <RegistrationInfo>
    <Date>2016-05-17T16:45:54.3423362</Date>
    <Author>NEED TO SET THIS</Author>
    <URI>RunLauncherTask</URI>
  </RegistrationInfo>

Code that a i use for create the task

$principal = New-ScheduledTaskPrincipal -UserId (Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -expand UserName)
$allTasks = Get-ScheduledTask | Select TaskName
$action = New-ScheduledTaskAction -Execute "C:\Launcher.exe"
$trigger = New-ScheduledTaskTrigger -AtLogOn
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal
$username = $principal.UserId
$taskName = 'RunLauncherTask' +  $username.Replace('\','-')
$settings = New-ScheduledTaskSettingsSet -DontStopIfGoingOnBatteries -AllowStartIfOnBatteries
Register-ScheduledTask $taskName -Action $action -Settings $settings

How do i set the autor?

Ivan Valadares
  • 869
  • 1
  • 8
  • 18

4 Answers4

6

Here is what worked for me.

After creating the task :

$taskObject = Get-ScheduledTask "Taskname"
$taskObject.Author = "authorname"
$taskObject | Set-ScheduledTask
bupmm
  • 61
  • 1
  • 2
  • 2
    In @Ivan's case he could also just do it while creating the task since the has the `$task` object. – Gup3rSuR4c Jun 24 '21 at 22:17
  • fantastic. what about the created date? – Matt May 24 '22 at 20:45
  • This seemed to work for me, but note: in my case, the job executes as a dedicated AD user account for services and automation, which I was not logged in as. It was necessary then to have the username and password so that the Set-ScheduleTask could complete without error (and not blow away the stored credentials). – Justin Oct 17 '22 at 14:08
1

Unfortunately, the only way to do this is via the -xml option.

Do note that the option takes the actual string, not a file name.

$xml = @"
....
....
"@
Register-ScheduledTask -Xml $xml -TaskName $task_name
mhhollomon
  • 965
  • 7
  • 15
  • this sucks because if a i use xml i can't set the current principal :( – Ivan Valadares Dec 13 '18 at 02:54
  • 1
    You can use Set-ScheduledTask to update it with the principal, Or, If I remember correctly, the prinicipal can be put in the xml itself. As soon as I get some time, I'll update the answer to show it (or correct this comment). it is definitely in the [xml schema](https://learn.microsoft.com/en-us/windows/desktop/taskschd/task-scheduler-schema) – mhhollomon Dec 13 '18 at 12:53
  • Set-ScheduledTask does the trick, i create a task via xml and then update the principal with Set-ScheduledTask . thanks – Ivan Valadares Dec 13 '18 at 15:30
  • It is not necessary to use the -xml option. See the answer https://stackoverflow.com/a/71952496/3095259 below – gerard Apr 21 '22 at 09:58
1

To elaborate on the answer from @bupmm and the corresponding comment from @Gup3rSuR4c, it is possible to set the author when setting up the scheduled task. Create the scheduled task object, set the author in the task object and then register the new task object (see code below).

$User = "user"
$Password = "password"
$TaskPath = "<scheduled_task_folder>"

$TaskName = "hello_world"
$TaskDescription = "Run Hello World"
$TaskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\source_code\scripts\hello_world.ps1"
$TaskTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration ([System.TimeSpan]::MaxValue)
$TaskPrincipal = New-ScheduledTaskPrincipal -Id $User -UserId $User -LogonType Password -RunLevel Limited
$Task = New-ScheduledTask -Description $TaskDescription -Action $TaskAction -Principal $TaskPrincipal -Trigger $TaskTrigger
$Task.Author = "Author"
$Task | Register-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -User $User -Password $Password | Out-Null
gerard
  • 835
  • 11
  • 27
0

I was looking for a way to change the author of existing tasks and was brought here by Google. This probably isn't the most efficient answer to OP's question, but I'll post it in case it helps other people in my situation.

Assuming the task(s) are already set up in Task Scheduler:

$tasks=Get-ScheduledTask |where author -like "<old_author>"

$tasks | Foreach-Object{

    $taskObject=Get-ScheduledTask $_.TaskName
    $taskObject.Author = "<new_author>"
    $taskObject | Set-ScheduledTask -User "<new_username>" -Password "<new_user_password>"
}
AffableAmbler
  • 377
  • 3
  • 15