Add file to HKEY_CURRENT_USER Autorun AND HKEY_LOCAL_MACHINE in only ONE script - vbscript

I have this VBScript:
On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")
myKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\example"
WshShell.RegWrite myKey, "C:\Program Files (x86)\example.exe", "REG_SZ"
Set WshShell = Nothing
which adds
"C:\Program Files (x86)\example.exe"
to the startup registry
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\example"
Now I also want to add
"C:\Program Files (x86)\example.exe"
to the startup registry
"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run\example"
How can you do that in only ONE SINGLE VBScript?
I tried to just modify this code:
On Error Resume Next
Set WshShell = CreateObject("WScript.Shell")
myKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\example"
WshShell.RegWrite myKey, "C:\Program Files (x86)\example.exe", "REG_SZ"
Set WshShell = Nothing
so that it would add the program to HKEY_LOCAL_MACHINE and then paste it underneath, but that didn't work.

Dim wshShell
Dim myKey
Set wshShell = CreateObject("WScript.Shell")
myKey = "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run\example"
wshShell.RegWrite myKey, "C:\Program Files (x86)\example.exe", "REG_SZ"

Related

Using Windows 8.1 and VBScript I need to create a folder in APPDATA. this is my code and I'm getting the error Path Not Found

I'm trying to create a folder in \username\appdata\local\ using Widows8.1 but the code I have is giving error path not found. please help!
Option Explicit
Dim oShell, oEnv, oFS, strDirectory, LocalAppData, wshshell
Set oShell = CreateObject("wscript.Shell")
set oFS = CreateObject("scripting.FileSystemObject")
Set oEnv = oShell.Environment("Process")
set wshshell = createobject("wscript.shell")
strDirectory = oEnv("USERPROFILE") & "\My Documents\MyConnect"
If Not oFS.FolderExists(strDirectory) Then oFS.CreateFolder(strDirectory)
I think My error is here
LocalAppData = wshshell.expandenvironmentstrings("%APPDATA%") & "\Local\MyConnectSettings"
If Not oFS.FolderExists (LocalAppData) Then oFS.CreateFolder (LocalAppData)
.CreateFolder won't create intermediate directories (other than mkdir). So make sure %appdata%\local exists.

VBScript to enable / disable proxy

I'm trying to create a script to go into startup applications which will automatically set the proxy in my workplace. The idea is this however I'm new to VBScript and I keep getting an error.
Option Explicit
Dim WSHShell, strSetting
ans = msgbox("Are you working in the office?" , vbyesno)
If ans = vbyes then
Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer", "10.2.2.88:8090", "REG_SZ"
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyOverride", "*.dev;*.test;*.local;<local>", "REG_SZ"
else
Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
End If
Can somebody show me where I'm going wrong?
Thanks!
Suppose your error is something as follows:
==>cscript //nologo D:\VB_scripts\SO\28832310.vbs
28832310.vbs(3, 1) Microsoft VBScript runtime error: Variable is undefined: 'ans'
Self-explanatory message, IMHO. As you have used Option Explicit statement correctly (forces explicit declaration of all variables in a script), simply add ans variable declaration, i.e. use
Dim WSHShell, strSetting, ans

how to delete multiple folders,desktop and start menu shortcut using vbscript

I never did any vbscript before, so i don't know if my question is very easy one. Following is the flow of steps that has to be done :
Check if exist and delete a folder at c:\test1 if found and continue. If not found continue.
Check if exist and delete a folder at c:\programfiles\test2 if found and continue. If not found continue.
Check if a desktop shortcut and start menu shortcut exist and delete if found. If not exit.
I could delete 2 folders with the following code:
strPath1 = "C:\test1"
strPath1 = "C:\test1"
DeleteFolder strPath1
DeleteFolder strPath1
Function DeleteFolder(strFolderPath1)
Dim objFSO, objFolder
Set objFSO = CreateObject ("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolderPath) Then
objFSO.DeleteFolder strFolderPath, True
End If
Set objFSO = Nothing
But i need to run one script to delete 2 folders in different paths, 2 shortcuts one in start menu and one on desktop.
I was experimenting with this code to delete the shortcut on my desktop:
Dim WSHShell, DesktopPath
Set WSHShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
on error resume next
Icon = DesktopPath & "\sample.txt"
Set fs = CreateObject("Scripting.FileSystemObject")
Set A = fs.GetFile(Icon)
A.Delete
WScript.Quit
It works fine for txt file on desktop, but how do i delete a shortcut for an application from desktop as well as start menu.
strPath1 = "C:\test1"
strPath2 = "C:\test2"
DeleteFolder strPath1
DeleteFolder strPath2
DeleteShortcut
'-------------------------------------------------------
Sub DeleteFolder(strFolderPath)
Set fso = CreateObject ("Scripting.FileSystemObject")
If fso.FolderExists(strFolderPath) Then
fso.DeleteFolder strFolderPath, True
End If
End Sub
'-------------------------------------------------------
Sub DeleteShortcut()
Set WSHShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WSHShell.SpecialFolders("Desktop")
shortcutPath = DesktopPath & "\MyShortcut.lnk"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(shortcutPath) Then
Set myFile = fso.GetFile(shortcutPath)
myFile.Delete
End If
End Sub

How to make VBScript copy permissions along with files

This is all in Windows XP utilizing VBScript.
I have a directory with several files inside. The files have varying permissions set. I need to be able to copy the files to a new directory while retaining the permissions. Using the script below the copy works fine but the permissions are overwritten by the new parent folder.
I am aware of xcopy but I am unsure how to make it work within the script. Using robocopy is a slight possibility but should be avoided if at all possible. Other utilities are out of the question due to network contraints.
Any help is greatly appreciated.
Dim CopyFromPath, CopyToPath
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
Const OverwriteExisting = TRUE
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE,"Select folder to copy:",NO_OPTIONS,ssfDRIVES)
if (not objFolder is nothing) then
Set objFolderItem = objFolder.Self
CopyFromPath = objFolderItem.Path
else
Set objShell = nothing
WScript.Quit(0)
end if
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, "Where should the folder be copied to?:", NO_OPTIONS, ssfDRIVES)
if (not objFolder is nothing) then
Set objFolderItem = objFolder.Self
CopyToPath = objFolderItem.Path
else
Set objShell = nothing
WScript.Quit(0)
end if
Set objFolder = nothing
Set objFolderItem = nothing
Set objShell = nothing
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile CopyFromPath & "\*.*", CopyToPath & "\", OverwriteExisting
msgbox "The folder has now been copied to " & CopyToPath
xcopy is a good idea for it.
An example to how make it work within vbscript.
Set oWSHShell = CreateObject("WScript.Shell")
oWSHShell.Exec "xcopy C:\source C:\destination /O /X /H /K /Y"
Set oWSHShell = Nothing

Change Wallpaper in Windows 7

I have written a small vbs script to download and change the registry for the current users wallpaper. however it copies and does the change but the wallpaper does not change... any ideas on errors in the below code?
Option Explicit
Dim WshShell, strValue, sleepTime, oFSO
strValue = "C:\wallpaper.bmp"
sleepTime = 30000
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CopyFile "\\anspksnms1\OSD\Scripts\wallpaper\wallpaper.bmp", "C:\"
Set WshShell = WScript.CreateObject("Wscript.Shell")
WshShell.RegWrite "HKCU\Control Panel\Desktop\Wallpaper", strValue
WScript.Sleep sleepTime
WshShell.Run "%windir%\System32\RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters", 1, False
Set WshShell = Nothing
I'm not much of a vbscripter, but maybe try this?
wshShell.run "cmd /c RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters", 1, True

Resources