launch VBS script after PC startup - vbscript

I want launch my simple VBS script after PC startup (Win XP). I don't want put this script in C:\Documents and Settings\%UserName%\Start Menu\Programs\Startup
I want do it in script, it is possible?
Script:<br>
Dim oShell<br>
Set oShell = WScript.CreateObject ("WScript.Shell")<br>
oShell.run "notepad.exe c:\text.txt"

You will either have to put it in the Startup folder or run it from the registry.
For all users, use registry key HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run.
For the current user, use registry key HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

In-depth
How to Automate VBScripts to run at startup.
Step 1
Click Start -> Run -> cmd or Click search and type cmd
Press enter
Type assoc .vbs in command prompt
Which should print .vbs=VBSFile
Type ftype VBSFile in command prompt
which should print:
vbsfile="%SystemRoot%\System32\WScript.exe" "%1" %*
So, now you know that your vbscript files open with WScript by default.
In command-prompt, type:
reg query HKEY_CLASSES_ROOT\Applications\WScript.exe /s
If you do not see this:
HKEY_CLASSES_ROOT\Applications\WScript.exe
(Standard) REG_SZ "%SystemRoot%\System32\WScript.exe" "%1" %*
Then you need to do the following, if the above is what you see, then you can skip and go to step 3:
Step 2
Go to:
Start
Run
Type in:
regedit
Select regedit press enter (or double-click regedit) and allow the
program to make changes to your computer
Navigate to:
HKEY_CLASSES_ROOT\Applications\WScript.exe (If WScript.exe key does not exist, right-click Applications and create new key, rename it to WScript.exe)
In the empty space on the right, where there are values, right-click
and
Choose new
Select String Value
Under Name where New Value #1 is highlighted, rename by typing
(Standard)
Under Data, double click the empty value and enter the value you got
from the previous step
"%SystemRoot%\System32\WScript.exe" "%1" %*
Step 3
If you do not have regedit open,
Go to:
Start
Run
Type in:
regedit
Select app, press enter and allow the program to make changes to your computer
Else, if regedit is open, then:
Navigate to:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Right click the run folder
in the empty space on the right, where there are values, right-click
and choose new
Select Expandable String Value
Under Name where New Value #1 is highlighted, rename by typing your
own name e.g. myscript
under Data, double click the empty value and enter this
Make sure its type is REG_EXPAND_SZ, i.e. an expanded string
"%SystemRoot%\System32\WScript.exe" "C:\Users\me\myFolder\mySub-folder\myFile.vbs" "%1" %*
Restart your machine. Your vbs should run automatically
Notes:
make sure .VBS is added to the Path environment variable
if you want to use cscript instead, In step 2 type:
reg query HKEY_CLASSES_ROOT\Applications\cscript.exe /s
...instead and proceed, taking note to replace WScript with cscript where relevant
i.e. this query:
reg query HKEY_CLASSES_ROOT\Applications\cscript.exe /s
Should produce this result:
(Standard) REG_SZ "C:\Windows\System32\cscript.exe" "%1" %*
If your key and/or value is messed up you can always right-click the
messed up item and delete it. If you however want to use the
terminal, you can always follow: http://ss64.com/nt/reg.html
To check if WScript is one of the startup apps, press ctrl+alt+delete, choose the Task Manager, click on Startup. You should see Microsoft Windows Based Script Host listed.

Easy Way :
Make a bat file
WScript.exe "Path\to\your\script.vbs"
add it to startup from gpedit.msc

Just an appoinment, everybody probalby just know already.
HKLM is for any user on the machine, because means Local Machine
HKCU is just for the current user.

Add this code to the start of your vbs script
Change the end of myKey to whatever you want to call the registry key
Set WshShell = CreateObject("WScript.Shell")
myKey = "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run\MyVbsScript"
WshShell.RegWrite myKey,WScript.ScriptFullName,"REG_SZ"

Task Scheduler Alternative
No script is needed, create a new task (Task Scheduler 1.0 (learn.microsoft.com) for instruction). Then "Create a task" and populate at least these details:
New Trigger: At Login
New Action -> Action: Start A Program
Program/Script: notepad.exe
Arguments: text.txt
Start in: c:\
Software: Windows 10, Task Scheduler 1.0

you can fire a vbscipt from registry or startup by
WScript C:\somefloder\somefolder2\yourscript.vbs
you can put this line in a value on
For all the users on the machine HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
for the current user HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

Related

run a cmd command as administrator via context menu on windows 10

I need some help: I want to create an explorer context menu option(while right clicking on a folder) for my program to run it as administrator from the context menu by using this .bat file:
#ECHO OFF
:: Choose the correct command processor for the current operating system
SET _cmd=
:: Variable to add shortcut to menu entry (NT only,
:: since COMMAND.COM cannot echo an ampersand)
SET _=
ECHO.%COMSPEC% | FIND /I "command.com" >NUL
IF NOT ERRORLEVEL 1 SET _cmd=command.com /e:4096
ECHO.%COMSPEC% | FIND /I "cmd.exe" >NUL
IF NOT ERRORLEVEL 1 SET _cmd=cmd.exe
IF [%_cmd%]==[cmd.exe] SET _=^&
:: Create a temporary .REG file
> %TEMP%.\DEFOPEN.REG ECHO REGEDIT4
>>%TEMP%.\DEFOPEN.REG ECHO.
:Print
ECHO Adding "Explorer context menu option" entry
>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\Directory\shell\ourprog]
>>%TEMP%.\DEFOPEN.REG ECHO #="%_%Add to Hot Backup (A.A.T Anti-Ransomware)"
>>%TEMP%.\DEFOPEN.REG ECHO.
>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\Directory\shell\ourprog\command]
>>%TEMP%.\DEFOPEN.REG ECHO #="java -jar C:\users\Tamir Naaman\Desktop\A.A.T-Anti-Ransomware.jar -backup \"%%L\"
>>%TEMP%.\DEFOPEN.REG ECHO.
:: If neither COMMAND.COM nor CMD.EXE then skip this step
IF [%_cmd%]==[] GOTO Merge
:: Merge the temporary .REG file
:Merge
START /WAIT REGEDIT /S %TEMP%.\DEFOPEN.REG
:: Delete the temporary .REG file
DEL %TEMP%.\DEFOPEN.REG
:: Ready
GOTO End
:: Clean up variables and quit
:End
SET _cmd=
SET _=
and I got this error message after I tried to click the option in the context menu:
error message
The registry picture:
Registry picture
The registry picture:
Registry picture
I just noticed that the default value is not set, how can i fix my code?
Can you help me find the problem in my batch file?
Thank you for your help!
Registry settings for 'Directory Shell Context Menu Command' to be executed As Administrator
For a context menu command to be executed As Administrator(Elevated) you have to put the command under the special verb RunAs like: [HKEY_CURRENT_USER\Software\Classes\Directory\shell\runas\command]
For directories, this special verb is usually unused, but you can never be sure about that, so using is not recommended at all.
The alternate recommended option is to use Static Cascading Menus, which has been introduced starting from Windows 7. With them, the context menu can be fully isolated with its own private RunAs verb.
A sample registry script leveraging Cascading Menus would be
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\AATCascade]
"ExtendedSubCommandsKey"="Directory\\shell\\AATCascade"
"MUIVerb"="A.A.T Anti-Ransomware"
"HasLUAShield"=""
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\AATCascade\shell\runas]
"HasLUAShield"=""
"MUIVerb"="Add to Hot Backup"
[HKEY_CURRENT_USER\Software\Classes\Directory\shell\AATCascade\shell\runas\command]
#="java.exe -jar \"C:\\Users\\Tamir Naaman\\Desktop\\A.A.T-Anti-Ransomware.jar\" -backup \"%V\""
You have to make sure that the location of java.exe is defined in the PATH environment variable or specify the full path to java.exe
The advantage of Cascading Menus is that you can add as many commands as you need (with custom verbs other than RunAs) and keep all of them under the root cascading menu resulting in a more user friendly and much cleaner context menu.
Under each sub menu there could be only one RunAs verb, So if there are more menu item command that needs elevation you have to elevate them by other means. And PowerShell can used for that task: PowerShell.exe -Command Start-Process "PathToExecutable" 'ExecutableParameters' -Verb RunAs
It is even possible to create nested cascading menus to keep the menu items more organized when you want add many more sub items and each sub menu can have its own RunAs verb.
After you have done testing the registry script then you can move on to the next step and adopt the registry script for importing from a batch file.
Embedding the registry script into a batch file
Three methods can be used each with its own pros and cons:
Converting the registry script to individual REG.EXE ADD commands. The advantage is that you don't need to escape the backslash(\) and double quotes(") like what is seen in registry scripts and it is more flexible in configuring the registry settings at runtime e.g. Determining the the location of the supporting files at runtime and adjusting the registry setting accordingly. But the conversion process is tedious and error-prone. Although there are tools out there than can automate the task and quickly convert a registry script to a batch file.
Writing the registry script to a temporary file at runtime using the echo commands like what you have done(with errors and mistakes of course). It provides some level of flexibility in adjusting the registry script at runtime with the help of environment variables, but you have to preserve the escaped syntax of the original registry script and also be careful about special characters in batch and escape them too. For small scripts this is not a problem but for bigger ones it gets out of management very quickly.
The batch script:
#echo off
(
echo REGEDIT4
echo,
echo [HKEY_CURRENT_USER\Software\Classes\Directory\shell\AATCascade]
echo "ExtendedSubCommandsKey"="Directory\\shell\\AATCascade"
echo "MUIVerb"="A.A.T Anti-Ransomware"
echo "HasLUAShield"=""
echo [HKEY_CURRENT_USER\Software\Classes\Directory\shell\AATCascade\shell\runas]
echo "HasLUAShield"=""
echo "MUIVerb"="Add to Hot Backup"
echo [HKEY_CURRENT_USER\Software\Classes\Directory\shell\AATCascade\shell\runas\command]
echo #=^"java.exe -jar \"C:\\Users\\Tamir Naaman\\Desktop\\A.A.T-Anti-Ransomware.jar\" -backup \"%%V\"^"
)>"%TEMP%\DEFOPEN.REG"
:: First try to import the registry script by REG.EXE to avoid unnecessary elevation if possible.
:: In case all the base keys are HKEY_CURRENT_USER no elevation is required.
REG IMPORT "%TEMP%\DEFOPEN.REG" 2>nul || REGEDIT /S "%TEMP%\DEFOPEN.REG"
del "%TEMP%\DEFOPEN.REG"
The last method is to copy paste the registry script at the end the following batch script. The advantage is that no special handling is required and the reg script can used as is. The disadvantage is that the registry script is static and batch script will not have control over the contents at runtime, it will be imported as it is.
Self Importer BAT/REG Script:
:: SelfImporter BAT/REG Script
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "TIME="
set "id=%TIME: =0%"
call :getBatFileInfo #f0 #nx0
set "id=%#nx0%.%id:~0,2%%id:~3,2%%id:~6,2%%id:~-2%"
set "RegFile=%TEMP%\RegImport.%id%.reg.tmp"
(
echo REGEDIT4
echo,
type "%#f0%"
)>"%RegFile%" && (
echo Importing registry script...
REG IMPORT "%RegFile%" 2>nul || REGEDIT /S "%RegFile%"
del "%RegFile%"
)
pause
goto :EOF
:getBatFileInfo <f0> <nx0>
set "%~1=%~f0" & set "%~2=%~nx0" & exit /b
<End of batch script>
Windows Registry Editor Version 5.00
The rest the registry script goes here
.
.
And at last you can just save your batch scripts with .CMD extension and forget about COMMAND.COM
To get the information I'm assuming you wanted into the registry your script should probably look a little more like this:
#ECHO OFF
( ECHO REGEDIT4
ECHO(
ECHO [HKEY_CURRENT_USER\Software\Classes\Directory\shell\ourprog]
ECHO #="Add to Hot Backup (A.A.T Anti-Ransomware)"
ECHO(
ECHO [HKEY_CURRENT_USER\Software\Classes\Directory\shell\ourprog\command]
ECHO #="java -jar \"C:\\Users\\Tamir Naaman\\Desktop\\A.A.T-Anti-Ransomware.jar\" -backup \"%%V\""
ECHO()>"%TEMP%\DEFOPEN.REG"
REGEDIT /S "%TEMP%\DEFOPEN.REG"
DEL "%TEMP%\DEFOPEN.REG"
It is your responsibility to ensure that the java command without specifying a full path will work fine in the target environment.
You will note that I have used the user key, because using the a key for every user on the PC is unnecessary especially when the jar file is located within a specific users profile tree

How to run a command on command prompt startup in Windows

EDIT
If you want to perform any task at computer startup or based on an
event this is very helpful
http://answers.microsoft.com/en-us/windows/forum/windows_7-performance/how-to-schedule-computer-to-shut-down-at-a-certain/800ed207-f630-480d-8c92-dff2313c193b
Back to the question
I have two questions:
I want some specific commands to be executed when I start command prompt.
e.g. cls to clear my command prompt.
I want to execute some commands in a batch file and wait for the user to enter new commands (if any).
e.g. A batch file which will take the user to some specified folder and then wait for the user to rename/delete a file from the command prompt.
How can I do it?
If you want a defined set of commands to run every time you start a command prompt, the best way to achieve that would be to specify an init script in the AutoRun registry value. Create it like this (an expandable string value allows you to use environment variables like %USERPROFILE%):
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun ^
/t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f
Then create a file init.cmd in your profile folder:
#echo off
command_A
command_B
...
cls
To remove these changes, delete the registry key:
reg delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun
Make a shortcut
Go to the properties
The bit where it says: C:\Users\<Your username>\Desktop\cmd.exe, you put: -cmd /K <your command here>
e.g.
C:\Users\Lewis\Desktop\cmd.exe -cmd /K color 1f
This is the way to launch 1 command without having to mess about with the registry.
Run multiple commands
You can also use & (and) operator to execute multiple commands.
Eg.
C:\Users\Lewis\Desktop\cmd.exe -cmd /K color 1f & H: & <your command>
Credits: user6589073
I found my answer: I should use the /K switch, using which I can enter a new command on the opened command prompt.
E.g. cmd /K cls will open a command prompt for me and clear it. (Answer for question 1)
and
cmd /K MyBatchFile.bat will start a command prompt, execute the batch file and stay on the command prompt and will not exit. (Answer for question 2).
First, you need to press Windows Key + R.
In the box that appears, type "regedit" (without the quotes).
The Windows Registry Editor should open.
Now, locate to HKEY_CURRENT_USER/Software/Microsoft/Command Processor.
Once you have clicked on Command Processor on the left side, click Edit on the top bar.
Then go to New > String Value in the Edit menu.
Rename the String Value that appears to Autorun.
Right click on Autorun and select Modify.
Under the "Value Data" area, type in the commands you want to run. You can run multiple by typing && between them.
Expanding a bit, here is an alternative for Windows 10 where multiple aliases can be defined and applied to the Command Prompt upon execution.
Create a file called init.cmd containing aliases on your %USERPROFILE% folder:
init.cmd
#echo off
doskey c=cls
doskey d=cd %USERPROFILE%\Desktop
doskey e=explorer $*
doskey g=git status
doskey l=dir /a $*
Register it to be applied whenever the Command Prompt is executed:
In the Command Prompt, run:
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f
Done
Now the contents of init.cmd will run for executions of cmd.exe namely from:
Taskbar shortcut
WIN+R cmd
By typing cmd in the File Explorer address bar
By running cmd.exe directly from C:\Windows\System32
After registering these settings just remember to close/open:
The Command Prompt so the settings are applied
The File Explorer, if you use to launch the cmd via File Explorer address bar
To unregister it, run:
reg delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun
I have a command to run a python program. I do not want to run this command manually after login, I want this command should run automatically after I logged in to my ubuntu. I am using Ubuntu 16.04.
Here is the command.
sh demo_darknet_yolov3.sh , this shell is placed in this directory littro#littro-System-Product-Name:~/MobileNet-YOLO-master/MobileNet-YOLO-master
Depending on your script, you may want to use the cmd.exe /k <input script> method instead of the registry entry Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\autorun
I have found with the latter, that other programs that launch cmd are affected by the registry entry. For example, I cannot get Visual Studio Native Tools prompt to work properly because my script gets in the way. In my case, the script is a menu with 5 options including to launch various programs manually (I like to minimize my auto-run programs) and set various environment variables (ie., printers, proxy settings, default versions of programs, etc).
If you are doing something static I think either method works fine.
I would have commented on the question or an applicable answer, but I do not have the reputation to comment.

Windows 7: cmd.exe: setting startup directory (in a link to cmd.exe)

I am running Windows 7 and when I run cmd.exe I want it to start up
in a directory named C:\foo\bar. I remember being able to create a
link to this executable on the desktop and right clicking somewhere
to set the startup menu of the cmd.exe command prompt by filling
out a field in a transient window, but I cannot find it. I have
found the following argument which however seems more complicated.
I want to set the startup directory for cmd.exe via a popup window.
Changing default startup directory for command prompt in Windows 7
Any ideas?
as mentioned by the other posters already:
the regular approach is to edit the shortcut's properties and fill the field labled "start in". simple as that.
however, for some reason this has no effect on UAC enabled systems if at the same time you also enable the "run as administrator" checkbox in the advanced properties of the shortcut.
a solution is to put everything in the "target" field of the shortcut:
%windir%\System32\cmd.exe /K cd /d "E:\My Folder" & sometest.bat
when running on 64bit and you want to explicitly start the 32bit flavour of the command prompt:
%windir%\SysWOW64\cmd.exe /K cd /d "E:\My Folder" & sometest.bat
for additional information on the command line parameters used above see:
cmd.exe /?
cd.exe /?
When you create a shortcut to cmd.exe, you can open the shortcut properties and find under Shortcut tab the Starts in option that will tell cmd.exe where to start, like here:
Open the properties of a shortcut to cmd and set the directory there:
Try this Shortcut Target:
C:\Windows\System32\cmd.exe cd /d %~dp0
Which will start cmd.exe in the shortcut's folder.

How to associate a file extension to a program without making it the default program

I'm deploying a small conversion tool on some systems, and want the users to be able to run it from the right click Open with menu. But I don't want to change the default program users have associated to this file type.
It is easy to associate a file extension/type to a program, but how to do it (programatically of course) without changing the default program?
Setting the following keys worked for me:
key HKLM/SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/<progname>: "" = <appPath>
key HKCR/Applications/<progname>/SupportedTypes: <fileExt> = ""
key HKCR/<fileExt>: "" = <progID>
key HKCR/<progID>/OpenWithList/<progName>
key HKCR/<fileExt>/OpenWithList/<progName>
key HKCR/SystemFileAssociations/<fileExt>/OpenWithList/<progName>
delete key and subkey at HKCU/SOFTWARE/Microsoft/Windows/CurrentVersion/Explorer/fileExts/<fileExt>
You can add scripts to the context menu (below Open with) by adding it in the windows registry:
Open regedit
Goto HKEY_CLASSES_ROOT\your_class\Shell
Add a new key and give it a name
Edit the (Default) value of this key and insert the text you want to show in the context menu
Add a new key named Command under your newly created key
Edit the (Default) value of this key and insert the command you want to execute
Enjoy!
In the "File Types" Windows Dialog you can click "Advanced" on your file type and there create a custom action tied to your application.
Possibly you can also find a way to do this in a programmatic manner, or at least create a .REG file with the equivalent registry options.
I have achieved the correct FILE ASSOCIATION using these cmd commands.
(just an example):
REG ADD "HKEY_CLASSES_ROOT\Applications\notepad++.exe\shell\open\command" /v # /t REG_SZ /d "\"C:\\Program Files\\Noteepad++\\notepad++.exe\" \"%1\"" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt" /v "Application" /t REG_SZ /d "notepad++.exe" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList" /v "g" /t REG_SZ /d "notepad++.exe" /f
assoc .txt=MyCustomType
ftype MyCustomType="C:\Program Files\Noteepad++\notepad++.exe" "%1"
(it's better to put them in .bat file)
here's a worked example for XP adding a command prompt option to folders. Create a .reg file
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\Command Prompt]
[HKEY_CLASSES_ROOT\Directory\shell\Command Prompt\command]
#="cmd.exe /k cd \"%1\""

cmd defaults to F: drive

When I open cmd on my laptop it is defaulting to the F: drive. This is troubling me does anyone know how it got that way or how to get it back to where it opens to the C: drive by default?
Use the command
C:
To change to the drive C. It would of course work for any drive letter.
Very minor nit: if you're using Windows 7 you don't need the cmdhere powertoy, it's built in to Explorer.
You just navigate to a directory in Windows Explorer then hold down the shift key and right click. "Open command window here" is one of the selections on the context menu.
When it comes to opening cmd.exe in a specific directory, I just create a shortcut to cmd.exe and then in the shortcut properties I set "Start in:" to the drive/directory I want it to start in.
Using a shortcut allows me to customize the cmd.exe windows depending on what I'm using it for. For normal file editing/viewing I use a 180x60 window and appropriate font, but when I want to read/search log files I have a shortcut that opens a 260x100 window with a smaller font. That way I can view most long log file lines without having to use the horizontal scroll.
http://blog.stevienova.com/2007/04/08/change-your-default-cmd-prompt-path/
Sometimes, your path when you go to start->run, CMD will be something
you don’t want. In active directory or on an NT domain, sometimes your
default home path might be a network drive. This isn’t so good when
you are offline or drop offline after being online. The CMD prompt is
set to a place where you can’t get to.
To change the path, you can edit the registry (at your own risk)
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor] “Autorun”=”c:”
This will change the path to your c: drive.
I believe it defaults to %HOMEDRIVE%\%HOMEPATH% so if you can muck about with those environment variables that might be an option. I can't edit these environment variables on my company's network, so I had to use the AutoRun to change it to something sane.
quick answer: cmd /k c:
long answer to make it "automagical":
http://windowsxp.mvps.org/autoruncmd.htm
In RegEdit.exe I created a String:
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
The value I used for AutoRun was "D:"
On the start screen / menu, type in "cmd", right-click it and select "Open File - Location".
In the opened window, right-Click on "Command Prompt" icon, select "Properties", and edit the "Start In" property to your desired path. I used "C:\" as an example
If you are opening it from a shortcut change the working dir for the shortcut.
In addition to the other answers, there's a nice powertoy for XP called "open command window here." It adds an option to your right-click context menu when you click inside a folder to open a command window using that directory as the starting path.
http://www.microsoft.com/windowsxp/Downloads/powertoys/Xppowertoys.mspx
I ran into a similar issue where cmd would always open up in a particular directory (annoying when running scripts which invoke cmd). The best way to deal with this is to edit your autorun settings. Raymond Chen has a nice article about this here:
http://blogs.msdn.com/oldnewthing/archive/2007/11/21/6447771.aspx
The summary is that when you start a command shell, it checks the autorun registry key, and executes the commands stored there. The registry keys it checks are:
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
and/or
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
Some answers already mentioned AutoRun as a solution.
But that can be very dangerous, as the AutoRun entry will be executed for any new cmd.exe instance (only pipes ignore the AutoRun).
A simple expample that fails:
cd /d E:\myPath
FOR /F "delims=" %%Q in ('dir') do echo - %%Q
With AutoRun=C:, this shows the content of the current path of drive C:
You can still use AutoRun, but it should be a batch script, that checks if it was called interactive, by FOR/F or by drag&drop.
#echo off
REM *** To enable this script, call it by <scriptName> --install
setlocal EnableDelayedExpansion
REM *** ALWAYS make a copy of the complete CMDCMDLINE, else you destroy the original!!!
set "_ccl_=!cmdcmdline!"
REM *** The check is necessary to distinguish between a new cmd.exe instance for a user or for a "FOR /F" sub-command
if "!_ccl_:~1,-2!" == "!comspec!" (
REM ***** INTERACTIVE ****
REM *** %1 contains only data, when the script itself was called from the command line
if "%~1" NEQ "" (
goto :direct_call
)
endlocal
doskey /macrofile="%~dp0\cmdMacros.mac"
echo ********************************************************************
echo * AutoRun executed from "%~f0"
echo * Macros loaded from "%~dp0\cmdMacros.mac"
echo ********************************************************************
cd /d C:\myPath
) ELSE (
REM *** Called by a FOR command, by an explorer click or a drag & drop operation
REM *** Handle PROBLEMATIC Drag&Drop content, if necessary
endlocal
)
exit /b
:direct_call
if "%~1" == "--install" (
reg add "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v "AutoRun" /t REG_SZ /d "%~f0"
exit /b
)
if "%~1" == "--show" (
reg query "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun
exit /b
)
if "%~1" == "--remove" (
reg DELETE "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun /f
)
exit /b

Resources