Run a PowerShell script on System Startup in the OOBE Sysprep environment.

Jacob Raffoul 1 Reputation point

Thanks for stopping by, I've searched the corners of the internet but haven't gotten anywhere.

To provision devices for my organization, we must manually run PowerShell commands using SHIFT + F10 in the Windows 11 OOBE as we have multiple methods, one of which being legacy. I'm sure there are better methods but I'm unfortunately working within these limitations. So far, to automate the imaging process, I've created an autounattend.xml which makes WinPE completely silent and some pages of the OOBE also.

Recently, I combined all the PowerShell commands we had been running prior into a script that, after running repeated checks for a network connection, prompts users with a GUI and effectively automates everything we had been doing manually before:

I need to make this run when the OOBE Sysprep starts, but I really need some help.

  • The script contains GUI, so it cannot run silently and the user needs to interact with it.
  • The script must start with the OOBE Windows Welcome Screen, (i.e. select region screen). This is a limitation of the modules used and I therefore can't include it as a synchronous command in FirstLogonCommands or include it in SetupComplete.cmd, as both are executed after the OOBE is completed.
  • I've tried configuring the answer file to boot into audit mode and have the script run there, but the script requires several reboots and I get an installation failed message after any reboot (despite later making the script enable the Administrator account and call "sysprep /audit /reboot"). Additionally, the Audit Administrator account takes ~15 minutes to log in so it defeats the whole purpose of time saving.
  • I've tried using Task Scheduler, testing triggers ;System Start Up; and 'User Log On', running as defaultuser0, BUILTIN\Administrators and SYSTEM. Task scheduler seems to either queue the task or not call it at all in the OOBE
  • I've tried placing the script, and then a shortcut of the script, in the common start up folder but that didn't work either.

To reiterate, I need a way to automatically run a script when the OOBE Sysprep starts. Furthermore, I need it to run every time the OOBE is launched as sometimes, we have to manually reboot if something glitches or goes wrong so the script will need to run again when the OOBE is resumed.

I know this is a tough one due to the limitations, but this will make the device rollout significantly easier.



Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
4,682 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Limitless Technology 37,746 Reputation points


    Thank you for your question and reaching out.

    I can understand you need to run PowerShell script during OOBE or WinPE.
    Please note that there is no Native method from Microsoft that you can run PowerShell script directly in Recovery or OOBE mode as it requires some dependencies.

    However you can use third party tools like OSDCloud to achieve this.

    --If the reply is helpful, please Upvote and Accept as answer--

  2. Akos 0 Reputation points

    Let’s check my solution here:

    0 comments No comments

  3. Jacob Raffoul 1 Reputation point

    Hey everyone, I found the answer around August last year so I'm sorry for the delayed response. You can get scripts to run during the OOBE using Logon scripts in gpedit (Local Machine Group Policy), you can programmatically create them with PowerShell. They will open behind the OOBE interface, so it is best to use a program like NirCMD to hide the process Window.

    0 comments No comments