Check Disabled state after Disable-ScheduledTask status on Running Task

Hello to all, i seek your wisdom!
I am trying to automate the following steps performed manually:

  • Disable a running scheduled task, but keeping it running.
  • Do actions that stop the task process.
  • Do additional actions.
  • Enable scheduled task and run it.

I can do all the steps in PowerShell, but the issue i have is that when i use:
Disable-ScheduledTask -TaskName ‘TaskName’ -TaskPath '\TaskPath'
while the task remains running, i am not able to get the Disabled state with:
Get-ScheduledTask -TaskName ‘TaskName’ -TaskPath '\TaskPath'
In the taskschd.msc ui i can see the option to enable the task, so i see hat is is disabled
But i need to confirm the Disabled state from PowerShell.
I am not able to find anything in task cim properties either.

I hope one of you knows the solution to my dilema.

Ondrej,
Welcome to the forum. :wave:t3:

Why?

I’d expect when this command …

… successfully ran wihtout error - the task is disabled. :man_shrugging:t3:

BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Hello Olaf,
thank you for your reply. Yes you are right, no error is good enough to me, but i need the script to be able to check the status for other tasks as well. Otherwise an sql instance would auto-start and job queue would populate again. Also i need to write logs.
However it is solved now. I posted also on MS tech community and i got pointed to the now obvious and convenient settings.enabled boolean property.

$task = Get-ScheduledTask -TaskName "AMDInstallLauncher" -TaskPath \
$task.Settings

AllowDemandStart                : True
AllowHardTerminate              : True
Compatibility                   : Vista
DeleteExpiredTaskAfter          :
DisallowStartIfOnBatteries      : False
Enabled                         : True
ExecutionTimeLimit              : PT1H
Hidden                          : True
IdleSettings                    : MSFT_TaskIdleSettings
MultipleInstances               : Queue
NetworkSettings                 : MSFT_TaskNetworkSettings
Priority                        : 3
RestartCount                    : 0
RestartInterval                 :
RunOnlyIfIdle                   : False
RunOnlyIfNetworkAvailable       : False
StartWhenAvailable              : False
StopIfGoingOnBatteries          : False
WakeToRun                       : False
DisallowStartOnRemoteAppSession : False
UseUnifiedSchedulingEngine      : False
MaintenanceSettings             :
volatile                        : False
PSComputerName                  :


$task.Settings.Enabled
True
PS > $task.Settings.Enabled.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Boolean                                  System.ValueType

The alternative that i found was to use the schtasks xml output and read the task.settings.enabled

[XML]$taskxml = schtasks /query /TN "\AMDInstallLauncher" /XML
$taskxml.Task.Settings.enabled

However, that string value only exists when the task is disabled and i wanted to stay in powershell commandlets.
So all good now :smiley:

1 Like