'How to add Author to a windows task using powershell
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?
Solution 1:[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
Solution 2:[2]
Here is what worked for me.
After creating the task :
$taskObject = Get-ScheduledTask "Taskname"
$taskObject.Author = "authorname"
$taskObject | Set-ScheduledTask
Solution 3:[3]
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>"
}
Solution 4:[4]
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
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 | mhhollomon |
Solution 2 | bupmm |
Solution 3 | AffableAmbler |
Solution 4 | gerard |