1

I'm trying to write code that puts itself in windows start-up and runs on boot using vbs. I wrote one that copies the file to the startup folder, but doesn't execute when the system boots.

Here is the code:

Set objShell = Wscript.CreateObject("Wscript.Shell")
strPath = objShell.SpecialFolders("Startup")
strMyPath = strPath&"\"  
Const SourceFile = "a.vbs"
strMyPath = strMyPath & SourceFile
Set fso = CreateObject("Scripting.FileSystemObject")

'Check to see if the file already exists in the destination folder
If fso.FileExists(strMyPath) Then

'Check to see if the file is read-only
If Not fso.GetFile(strMyPath).Attributes And 1 Then 
    'The file exists and is not read-only.  Safe to replace the file.
    fso.CopyFile SourceFile, strMyPath, True
Else 
    'The file exists and is read-only.
    'Remove the read-only attribute
    fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes - 1
    'Replace the file
    fso.CopyFile SourceFile, strMyPath, True
    'Reapply the read-only attribute
    fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes + 1
End If
Else
'The file does not exist in the destination folder.  Safe to copy file to this folder.
fso.CopyFile SourceFile, strMyPath, True
End If
Set fso = Nothing
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Bilal
  • 558
  • 8
  • 18

1 Answers1

0

This script can be executed to inject a vbs file into the HKLM\startup path or the HKCU\startup path. The difference being that HKLM applies to all users, where as HKCU only applies to you, the current user activating this script.

Const cHKLM = &H80000002
Const cComp = "."
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
cComp & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
strValueName = "ScriptName"
strValue = "Drive:\PathTo\Myscript.vbs"
objReg.SetStringValue cHKLM, strKeyPath, strValueName, strValue

Good Luck

Rich
  • 4,134
  • 3
  • 26
  • 45