6

I want to create the command hello without having to create a file like hello.bat. I want the command without the file. The command must execute an echo Hello World!.

Is there a way to do it on Windows?

  • 10
    Just curious, but why? Ultimately it will still be held in a file somewhere, just not one you're controlling directly. I'm very curious as to what the purpose of this objective is. – Patrick L Jul 1 at 2:02
  • 8
    My psychic power detect an XY question: I'm guessing that the OP has a bat/cmd which calls hello.bat, but the hello.bat call never returns. The solution for that is to use call hello.bat, rather than eliminating hello.bat. – Jonathan Jul 1 at 7:05
  • I have like 5 .bats and I was wondering if I could get rid of them and still use the 5 commands. – Twinsen Jul 2 at 17:17
13

I want to create the command hello without having to create a file

You can use doskey.

  1. Open a cmd shell

  2. Enter the following command:

    doskey hello=echo Hello world!
    
  3. Run the command:

    hello
    

Example:

F:\test>doskey hello=echo Hello world!

F:\test>hello
Hello world!

F:\test>

Further Reading

  • doskey - Recall and edit commands at the DOS prompt, and create macros. You cannot run a Doskey macro from a batch file.
| improve this answer | |
  • 6
    Note that this only works within cmd, e.g. it won't work from PowerShell, etc.. Much like a bash alias, it's shell-specific. – Bob Jul 1 at 1:56
7

You can create an Environment variable, and then call it using %hello%

![![enter image description here

![enter image description here

![enter image description here

enter image description here

enter image description here

| improve this answer | |
  • 4
    Consider adding the text instruction of the steps to go along with the complimentary screen shots in case the screen shot links ever break, then there will be a lot of detail missing. – Pimp Juice IT Jun 30 at 17:44
  • 4
    In support of the comment from @PimpJuiceIT, every image has a description of enter image description here which makes them somewhat opaque to screen reader software – Rob Jul 1 at 4:31
0
  • For add/create one system variable without using/following GUI:

In command line:

Using setx command:

rem :: For current user (save in "HKEY_CURRENT_USER")
setx Hello "echo Hello world!"

rem :: For all users (save in "HKEY_LOCAL_MACHINE")
setx Hello "echo Hello world!" /m

Using reg add command:

rem :: For current user (save in "HKEY_CURRENT_USER")
reg add HKCU\Environment /v Hello /d "echo Hello world!" /f

rem :: For all users (save in "HKEY_LOCAL_MACHINE")
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Hello /d  "echo Hello world!" /f

  • In File.reg

For current user:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Environment]
"Hello"="echo Hello world!"

For all users:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
"Hello"="echo Hello world!"

Using:

C:\>%hello%
Hello world!


For removing by command line:

For current user:

reg delete HKCU\Environment /v Hello /f

For all users:

reg delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Hello /f

For removing by File.reg

For current user:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Environment]
"Hello"=-

For current all users:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
"Hello"=-
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.