Windows – Add Runonce Key

email me

Manually in HKEY_CURRENT_USER

REG ADD HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v RunScript /t REG_SZ /d “C:\setup\script.cmd” /f

 

Manually in HKEY_USERS

REG ADD HKU\S-1-5-21-3492930481-2827506072-2794401122-1001\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v RunScript /t REG_SZ /d “C:\setup\script.cmd” /f

 

HKEY_CURRENT_USER via Script (or…SCCM)

REG LOAD HKU\TEMP “C:\Users\Default\ntuser.dat”
REG ADD HKU\TEMP\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v RunScript /t REG_SZ /d “C:\setup\script.cmd” /f
REG UNLOAD HKU\TEMP

 

All Profiles via Load and Unload via Script

FOR /D %%D IN (“C:\Users\*”) DO IF EXIST “%%D\NTUSER.DAT” REG LOAD HKU\TEMP “%%D\NTUSER.DAT” && (REG ADD HKU\TEMP\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v Reg_Value /t REG_SZ /d Reg_Data /f & REG UNLOAD HKU\TEMP)

 

Using PowerShell

$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce"
$regKey = "RunScript"
$regValue = "c:\setup\script.cmd"

IF(!(Test-Path $regPath))

{

New-Item -Path $regPath -Force | Out-Null

New-ItemProperty -Path $regPath -Name $regKey -Value $regValue -PropertyType STRING -Force | Out-Null}

ELSE {

New-ItemProperty -Path $regPath -Name $regKey -Value $regValue -PropertyType STRING -Force | Out-Null}

 

Using PowerShell App Deployment Toolkit

[scriptblock]$HKCURegistrySettings = {
Set-RegistryKey -Key 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce' -Name 'RunScript' -Value '"c:\setup\script.cmd"'-Type String -ContinueOnError:$True
}
Invoke-HKCURegistrySettingsForAllUsers -RegistrySettings $HKCURegistrySettings

 

Using C# in Visual Studio


using Microsoft.Win32; //used by Registry

namespace PlayingAround
{
class WriteReg
{
public static void Main(string[] args)
{
// define variables
const string userRoot = "HKEY_CURRENT_USER";
const string subkey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
const string keyName = userRoot + "\\" + subkey;

// set registry key
Registry.SetValue(keyName, "RunScript", "C:\\setup\\script.cmd", RegistryValueKind.String);

}
}
}

 

Using AutoIt in SciTE-Lite

WriteReg()

Func WriteReg()
  ; Write key
  RegWrite("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce", "RunScript", "REG_SZ", "C:\setup\script.cmd")
EndFunc ;==>WriteReg