简体   繁体   中英

Start a powershell script at the end of Windows 10 installation?

I have a custom ISO image and I would like at the end of its installation, before logging in to the user, to run my PowerShell script.

I've seen some custom ISOs that do this. How is it done?

Before capturing your WIM image, you can set up a script to run on first boot in a few different ways. There's a lot of detail and examples here to read: Add a custom script to Windows setup . I would recommend one of these options:

  1. Simple CMD Create a script here: %WINDIR%\Setup\Scripts\SetupComplete.cmd . This script runs with local system permissions and starts immediately after the user sees the desktop.

  2. More Complex Create an Unattend.xml file with the FirstLogonCommands\SynchronousCommand setting to run after the Out of Box Experience (OOBE) but before the user sees the desktop.

  3. Powershell Create a scheduled task set to run once at startup, then copy your script to C:\Windows\Tasks\MyScript.ps1 :

# schedule to run at next startup
$trigger = New-ScheduledTaskTrigger -AtStartup

# first, it runs your script
$action1 = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-F "C:\Windows\Tasks\MyScript.ps1"'
# then it removes this task when done so it doesn't run again
$action2 = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-C Unregister-ScheduledTask -TaskName "MyTask" -Confirm:$false'

# run as local admin
$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest

# finally, register everything and you're done!
Register-ScheduledTask -TaskName 'MyTask' -Action $action -Trigger $trigger -Principal $principal -Description 'My task to run after first startup' -ErrorAction Stop

Capture wim after doing one of the above, and you should be set.

If you want to use an unattend.xml script, you must run c:\windows\system32\sysprep /generalize /oobe /unattend:autounattend.xml /shutdown, and before the shutdown, generate the wim image booting from winPE or similar tool.

This way your wim image is ready to deploy in any other machine, and the script you selected inside the autounattend.xml will run on boot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM