I'm deploying VS Code with a baseline configuration to several high-school computer labs. During installation, I create an Active Setup registry entry that adds a per-user HKCU RunOnce value. That RunOnce command should launch a short PowerShell script, which copies a preset settings.json from a hidden folder on C: to the current user's AppData directory.
The command works when the script path contains no spaces:
REG ADD HKCUSoftwareMicrosoftWindowsCurrentVersionRunOnce /v VSCodeCopy /t REG_SZ /d "C:WindowsSystem32WindowsPowerShellv1.0powershell.exe -ExecutionPolicy Bypass -File C:MyPathVSCode_CopySettings.ps1"
However, it fails when the script is located under a path such as C:My Path. I tried single quotes, double quotes, and several escaping variations. PowerShell appears to launch and then close immediately, and even adding Read-Host inside both the try and catch blocks does not pause the window.
The RunOnce command is being created by the Active Setup StubPath value, so I'm using reg.exe rather than creating the value directly from PowerShell. The goal is for both existing and new users to run the setup once at login, with a version value allowing the configuration to be applied again later if needed. What is the correct quoting format, and is there a better way to structure this command?
2 Answers
The important difference between the two examples is not the path length; it is how the path is quoted. The argument after -File should be one quoted Windows path, using double quotes: -File "C:My PathVSCode_CopySettings.ps1". Do not use PowerShell-style single quotes as the quoting mechanism for a command that will be parsed by cmd.exe and RunOnce.
You can also shorten the command by using powershell.exe instead of its fully qualified path. For a more maintainable deployment, consider registering a logon-triggered scheduled task instead of chaining Active Setup, cmd.exe, and RunOnce. A scheduled task gives you clearer control over the user context, hidden execution, logging, and repeat behavior.
The script path itself needs to be enclosed in double quotes in the command stored in RunOnce. Single quotes are not reliable here because RunOnce launches the command through the Windows command-line parser, which can pass those quote characters literally to PowerShell.
The stored value should look like this:
powershell.exe -ExecutionPolicy Bypass -File "C:My PathVSCode_CopySettings.ps1"
If you create that value with reg.exe, the outer quotes are for reg.exe’s /d argument and the inner quotes must be escaped appropriately. It can be easier to create the RunOnce value with PowerShell instead:
Set-ItemProperty 'HKCU:SoftwareMicrosoftWindowsCurrentVersionRunOnce' -Name VSCodeCopy -Type String -Value 'powershell.exe -ExecutionPolicy Bypass -File "C:My PathVSCode_CopySettings.ps1"'
Also, there is normally no need to use the full path to powershell.exe; it should already be available through PATH. If possible, test the exact string saved in the registry rather than only testing the command interactively.

The complication is that Active Setup runs the StubPath through cmd.exe, so creating the RunOnce value with PowerShell adds another layer of quoting and makes the command unnecessarily long. That is why I was trying to keep the registry operation in reg.exe.