Sysprep from PowerShell, is it possible?

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.

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’


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

C:\Windows\System32\Sysprep\Sysprep.exe /generalize /oobe /shutdown /quiet

try this

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


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

Mohamed Nazar

other possible alternative :slight_smile:

$cmd = 'C:\Windows\System32\Sysprep\Sysprep.exe'
$args = '/generalize','/oobe','/shutdown','/quiet'
& $cmd $args

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: Sysprep - Fatal error occurred while trying to sysprep the machine

Mohamed Nazar