{"id":1843,"date":"2025-02-25T08:30:00","date_gmt":"2025-02-25T07:30:00","guid":{"rendered":"https:\/\/kasperjohansen.net\/?p=1843"},"modified":"2025-02-25T08:53:24","modified_gmt":"2025-02-25T07:53:24","slug":"quickpost-create-a-scheduled-task-to-run-in-user-context","status":"publish","type":"post","link":"https:\/\/kasperjohansen.net\/index.php\/2025\/02\/25\/quickpost-create-a-scheduled-task-to-run-in-user-context\/","title":{"rendered":"Quickpost &#8211; Create a scheduled task to run in user context"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">From time to time, it&#8217;s required to &#8220;do something&#8221; in user context on a modern managed cloud only Windows device. Installing apps and pushing policies via Microsoft Intune is easy enough, it&#8217;s part of the Intune UI to select either system or user context when creating a new application or selecting specific user policies in the Session Catalog and assigning configuration profiles to a group of users.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say you want to execute a particular task in the user&#8217;s context every time the user logs into Windows, it could be to start an application or maybe execute a script that would cleanup temporary or cached data in the user&#8217;s profile, this is not something we currently have as a built in feature in Intune.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have the Platform Script feature, which is great for at kind of &#8220;set it and forget it&#8221; solution, where the script is executed once and not executed again<a href=\"https:\/\/learn.microsoft.com\/en-us\/mem\/intune\/apps\/intune-management-extension#before-you-begin\"> unless there are changes in the script or the script policy<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"395\" src=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-scripts-01-1024x395.jpg\" alt=\"\" class=\"wp-image-1962\" srcset=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-scripts-01-1024x395.jpg 1024w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-scripts-01-300x116.jpg 300w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-scripts-01-768x296.jpg 768w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-scripts-01-1536x593.jpg 1536w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-scripts-01.jpg 1918w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_86 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/kasperjohansen.net\/index.php\/2025\/02\/25\/quickpost-create-a-scheduled-task-to-run-in-user-context\/#A_scripted_solution\" >A scripted solution<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/kasperjohansen.net\/index.php\/2025\/02\/25\/quickpost-create-a-scheduled-task-to-run-in-user-context\/#The_Powershell_script\" >The Powershell script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/kasperjohansen.net\/index.php\/2025\/02\/25\/quickpost-create-a-scheduled-task-to-run-in-user-context\/#The_JSON_file\" >The JSON file<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/kasperjohansen.net\/index.php\/2025\/02\/25\/quickpost-create-a-scheduled-task-to-run-in-user-context\/#Configure_Win32_app_in_Intune\" >Configure Win32 app in Intune<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_scripted_solution\"><\/span>A scripted solution<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">I decided to create a script that would register a scheduled task to run in user context at logon. This script is based on Powershell  and the scheduled task parameters are configured via a JSON file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I have published the script in my Github, feel free to download it a play around with it:<br><a href=\"https:\/\/github.com\/kaspersmjohansen\/Microsoft-Intune\/tree\/master\/Scripts\/Create%20Scheduled%20Task\">Microsoft-Intune\/Scripts\/Create Scheduled Task at master \u00b7 kaspersmjohansen\/Microsoft-Intune<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Powershell_script\"><\/span>The Powershell script<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s have a look at the Powershell script, ScheduledTask.ps1. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The script supports a handful of parameters, where the -ConfigFile and -TaskStatus parameters are mandatory.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Powershell-script-01-1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"200\" src=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Powershell-script-01-1.jpg\" alt=\"\" class=\"wp-image-1971\" srcset=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Powershell-script-01-1.jpg 700w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Powershell-script-01-1-300x86.jpg 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">With the -ConfigFile parameter the script is instructed to read the contents of a JSON configuration file. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The -Taskstatus instructs the script to either register (create) a scheduled task or unregister (remove) a scheduled task, based on the information in the JSON file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The command might look like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ScheduledTaskUser.ps1 -ConfigFile ScheduledTaskConfig.json -TaskStatus Register<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will register (create) a scheduled task based on the information in the ScheduledTaskConfig.json file. Furthermore, a log file is created in the ScheduledTask folder in the user&#8217;s profile.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_JSON_file\"><\/span>The JSON file<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In this example my JSON file is called ScheduledTaskConfig.json, the name is not important, you can call it what you want. I recommend that you provide a name that would help identify what task JSON file associated with.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the JSON file configured the TaskName, TaskDescription, TaskPath, TaskAction, TaskActionArguments and TaskActionWorkingDirectory<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n    \"ScheduledtaskInfo\":{\n                    \"TaskName\":  \"LaunchEdgeBrowser\",\n                    \"TaskDescription\":  \"Launch Microsoft Edge at logon\",\n                    \"TaskPath\":\"\\\\CustomScheduledTasks\",\n                    \"TaskAction\": \"C:\\\\Program Files (x86)\\\\Microsoft\\\\Edge\\\\Application\\\\msedge.exe\",\n                    \"TaskActionArguments\": \"--profile-directory=Default\",\n                    \"TaskActionWorkingDirectory\": \"C:\\\\Program Files (x86)\\\\Microsoft\\\\Edge\\\\Application\"\n                }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Executing the script with the JSON will register a scheduled task with this configuration:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-01.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"220\" src=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-01-1024x220.jpg\" alt=\"\" class=\"wp-image-1973\" srcset=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-01-1024x220.jpg 1024w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-01-300x64.jpg 300w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-01-768x165.jpg 768w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-01.jpg 1253w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The scheduled task is registered in the CustomScheduledTasks folder<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-02.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"632\" height=\"480\" data-id=\"1974\" src=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-02.jpg\" alt=\"\" class=\"wp-image-1974\" srcset=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-02.jpg 632w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-02-300x228.jpg 300w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-03.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"634\" height=\"480\" data-id=\"1975\" src=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-03.jpg\" alt=\"\" class=\"wp-image-1975\" srcset=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-03.jpg 634w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-03-300x227.jpg 300w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-04.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1003\" height=\"481\" data-id=\"1976\" src=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-04.jpg\" alt=\"\" class=\"wp-image-1976\" srcset=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-04.jpg 1003w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-04-300x144.jpg 300w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/ScheduledTask-04-768x368.jpg 768w\" sizes=\"auto, (max-width: 1003px) 100vw, 1003px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The scheduled will run in the context of the user at logon, and in this case the Microsoft Edge browser is launched. Upon a successful registration of the scheduled task, the script will create a .tag file in the folder configured as the log folder. By default, the log folder is in the user\u2019s profile folder, this tag file can be used as a detection method in Intune. If the scheduled task registration fails, the .tag file is not created.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Configure_Win32_app_in_Intune\"><\/span>Configure Win32 app in Intune<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The last thing to do is to convert the script to a win32 app and configure it in Intune.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example of how to configure the install and uninstall commands:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"709\" src=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-win32-01-1024x709.jpg\" alt=\"\" class=\"wp-image-1978\" srcset=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-win32-01-1024x709.jpg 1024w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-win32-01-300x208.jpg 300w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-win32-01-768x532.jpg 768w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-win32-01.jpg 1098w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Make sure to configure the install behavior to run in user context:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"781\" height=\"238\" src=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-win32-03.jpg\" alt=\"\" class=\"wp-image-1979\" srcset=\"https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-win32-03.jpg 781w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-win32-03-300x91.jpg 300w, https:\/\/kasperjohansen.net\/wp-content\/uploads\/2025\/01\/Intune-win32-03-768x234.jpg 768w\" sizes=\"auto, (max-width: 781px) 100vw, 781px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This command will register the scheduled task:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">%SystemRoot%\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe -NoLogo -WindowStyle Hidden -Executionpolicy \"Bypass\" -File \"ScheduledTaskUser.ps1\" -ConfigFile \"ScheduledTaskConfig.json\" -TaskStatus Register<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command will unregister the scheduled task:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">%SystemRoot%\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe -NoLogo -WindowStyle Hidden -Executionpolicy \"Bypass\" -File \"ScheduledTaskUser.ps1\" -ConfigFile \"ScheduledTaskConfig.json\" -TaskStatus Unregister<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This concludes the article. Feel free to reach out to me on\u00a0<a href=\"https:\/\/twitter.com\/KasperMJohansen\">X<\/a>\u00a0or on\u00a0<a href=\"https:\/\/www.linkedin.com\/in\/kaspermjohansen\/\">LinkedIn<\/a>\u00a0if you have any comments or questions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From time to time, it&#8217;s required to &#8220;do something&#8221; in user context on a modern managed cloud only Windows device. Installing apps and pushing policies via Microsoft Intune is easy &#8230;<\/p>\n","protected":false},"author":2,"featured_media":1810,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,32],"tags":[],"class_list":["post-1843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-intune","category-scripts"],"_links":{"self":[{"href":"https:\/\/kasperjohansen.net\/index.php\/wp-json\/wp\/v2\/posts\/1843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kasperjohansen.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kasperjohansen.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kasperjohansen.net\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kasperjohansen.net\/index.php\/wp-json\/wp\/v2\/comments?post=1843"}],"version-history":[{"count":12,"href":"https:\/\/kasperjohansen.net\/index.php\/wp-json\/wp\/v2\/posts\/1843\/revisions"}],"predecessor-version":[{"id":1981,"href":"https:\/\/kasperjohansen.net\/index.php\/wp-json\/wp\/v2\/posts\/1843\/revisions\/1981"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kasperjohansen.net\/index.php\/wp-json\/wp\/v2\/media\/1810"}],"wp:attachment":[{"href":"https:\/\/kasperjohansen.net\/index.php\/wp-json\/wp\/v2\/media?parent=1843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kasperjohansen.net\/index.php\/wp-json\/wp\/v2\/categories?post=1843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kasperjohansen.net\/index.php\/wp-json\/wp\/v2\/tags?post=1843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}