Sysprep from PowerShell, is it possible?

Tagged: 

This topic contains 6 replies, has 6 voices, and was last updated by Profile photo of Mohamed Nazar Mohamed Nazar 3 weeks, 2 days ago.

  • Author
    Posts
  • #18784
    Profile photo of i255d
    i255d
    Member

    I see that Microsoft says that you can run Sysprep from the command line, but I can't seem to get it to work on my 2012 r2 server. Not a cmdlet in Powershell and not a recognized command from the command line. I even tried going to the executable, no such luck. The only thing that seem to be getting me close is:

    $command = "C:\Windows\System32\Sysprep\Sysprep.exe /generalize /oobe /shutdown /quiet"
    Invoke-Expression -command "$command"

    but then I get, " If no command-line arguments are provided a graphical user interface is used to select the desired Sysprep operstions."

    I am trying to deploy a 3 vm's and script it all out for future use. I have made it through a few big blocks so far.

  • #18785
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Hi Dan,

    The below PowerShell command works for me on 2012 R2.

    Start-Process -FilePath C:\Windows\System32\Sysprep\Sysprep.exe -ArgumentList '/generalize /oobe /shutdown /quiet'

    Best,
    Daniel

  • #18816
    Profile photo of Aaron Jensen
    Aaron Jensen
    Participant

    Why are you using Invoke-Expression? What happens when you run:

    C:\Windows\System32\Sysprep\Sysprep.exe /generalize /oobe /shutdown /quiet
    
  • #18818
    Profile photo of H Man
    H Man
    Participant

    try this

    $sysprep = 'C:\Windows\System32\Sysprep\Sysprep.exe'
    $arg = '/generalize /oobe /shutdown /quiet'
    $sysprep += " $arg"
    Invoke-Expression $sysprep
    
  • #57698
    Profile photo of Mohamed Nazar
    Mohamed Nazar
    Participant

    Hi

    When I run the below commands one by one, sysprep is working fine, but when I execute below commands as a file the sysprep is not executing. Sysprep popup window opened and closed for a short time.

    $sysprep = 'C:\Windows\System32\Sysprep\Sysprep.exe'
    $arg = '/generalize /oobe /shutdown /quiet'
    $sysprep += " $arg"
    Invoke-Expression $sysprep

    Please help to resolve it

    Regards,
    Mohamed Nazar

  • #57712
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    other possible alternative 🙂

    $cmd = 'C:\Windows\System32\Sysprep\Sysprep.exe'
    $args = '/generalize','/oobe','/shutdown','/quiet'
    & $cmd $args
    
  • #57716
    Profile photo of Mohamed Nazar
    Mohamed Nazar
    Participant

    Hi Max, I tried the command but still sysprep is not triggered.

    I removed "Quiet" argument and tried and found that the actual issue.

    The issue is "a fatal error occured while trying to sysprep the machine"

    I just followed the below steps and now sysprep is working fine.

    >>>>
    Start Search box to open Registry Editor, set the value of GeneralizationState under HKEY_LOCAL_MACHINE\SYSTEM\Setup\Status\SysprepStatus to 7, then run sysprep again.
    >>>>
    Source: https://social.technet.microsoft.com/Forums/office/en-US/425bd101-8a87-488c-b2e2-9f2f8113a0d9/sysprep-fatal-error-occurred-while-trying-to-sysprep-the-machine?forum=w7itproinstall

    Regards,
    Mohamed Nazar

You must be logged in to reply to this topic.