'Change the setting 'StartWhenAvailable' via Powershell

I have some scheduled tasks where I'm wanting to set the following setting to true

StartWhenAvailable which correlates to this setting enter image description here

What is the best way in Powershell to achieve this? I wrote the following, but I feel like there is a much better way to do it.

$update = New-ScheduledTaskSettingsSet -StartWhenAvailable 
Set-ScheduledTask -TaskName 'Adobe Acrobat Update Task' -Settings $update

Also, how do you change these values to true/false?

enter image description here



Solution 1:[1]

For your first question, that's a perfectly valid way to set the Settings object. Another way would be to operate on the objects themselves, which might make sense if you're looping over a series of objects, or similar.

$Task = Get-ScheduledTask -TaskName 'Adobe Acrobat Update Task'
$Settings = $Task.Settings
$Settings.StartWhenAvailable = $true
Set-ScheduledTask -TaskName 'Adobe Acrobat Update Task' -Settings $Settings

For your second question, the -StartWhenAvailable is a [switch] in PowerShell - see docs. You can toggle the settings to $false by specifying -StartWhenAvailable:$false. See switch parameters for more details.

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 kcamp