r/PowerShell • u/DefinitionHuge2338 • Aug 27 '26
Question Issue with Active Setup + RunOnce and space in script path
I'm attempting to deploy VSCode with a baseline configuration to some high school computer labs, and am running into an odd issue.
As part of the install, I create an Active Setup registry key that creates a HKCU RunOnce key to call a short powershell script. That script copies a preset settings.json from a hidden folder on the C:\ drive to the current user's AppData folder.
The issue I'm running into is that if the path to the script contains a space, I cannot get the RunOnce key to work properly (it's not over 260 chars either).
Working Active Setup key:
REG ADD HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce /v VSCodeCopy /t REG_SZ /d "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File C:\MyPath\VSCode_CopySettings.ps1"
Non-working Active Setup key:
REG ADD HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce /v VSCodeCopy /t REG_SZ /d "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File 'C:\My Path\VSCode_CopySettings.ps1'"
I have tried double quotes, single quotes, escaping quotes using \ and "", but I cannot get the script to function unless the script path has 0 spaces in it. RunOnce will execute powershell, but the terminal will just open and close rapidly.
I added a Read-Host in both halves of a try-catch block in the script to confirm what's happening, and neither causes the terminal window to wait for input.
Is there something I'm missing?
Edit: I'm combining Active Setup and RunOnce in order to not bog down the user login; the command to create an HKCU key is a string value in an Active Setup registry key, rather than being run by the install script, which has some limitations. However, it means I can guarantee that old and new users will have the script run a single time on login. I can also use a 'Version' registry value to make it happen again in the future if the initial configuration needs updated.
2
u/purplemonkeymad Aug 27 '26
It's probably the lack of double quotes in the key itself. I would create the key in powershell as well instead of relying on reg.exe:
Note the location of single and double quotes.
1
u/SarcasticFluency Aug 27 '26
You would be better off using New-ItemProperty to create the destination if it's absent and Set-ItemProperty. Define the script into its own variable with the actual command you want to run as the value in Set-ItemProperty.
1
u/StartAutomating Aug 27 '26
I think you're answering your own questions without realizing it.
Additionally, I feel like many of the people replying to this thread are pointing you in the right direction, and you are explaining why they are wrong in this scenario, rather than accepting the joys of Occam's Razor.
The simplest explanation of what is going wrong is that
-fileshould not be quoted.The most likely explanation of what is going wrong is a bit of PEBCAK.
My apologies for the shortness in tone here: I am currently (literally) sick.
Please start by removing the quotes from
-fileand shortening the path.Good luck!