Search code examples
powershellregistryrunonce

Running PowerShell from RunOnce


I am trying to run a set of powershell commands during a template customization. These commands are injected into the RunOnce in the registry.

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command &{Add-Type -Assembly System.Web; [Web.Security.Membership]::GeneratePassword(21,7) | Select-Object @{N="Group";E={"Test Group"}}, @{N="Username";E={"TestUser"}}, @{N="Password";E={$_}} | Export-Csv -Path C:\stuff\user.csv -NoTypeInformation}

I know that these commands work, as I can run them in a powershell console. I know that "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command &{PSCODE}" is correct because other runonce commands will run correctly. I cannot figure out what part of my syntax is failing.


Solution

  • This could be a long trial and error session, to save you precious time, I suggest you use a script file instead of that long command:

    %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File c:\script.ps1