99

Just discovered doskey emacs=emacs -nw $*, but without something like .bashrc, I'll have to type that every time I start a shell, right?

2

11 Answers 11

98

This is a very good question. I found this. I suppose you could make a cmd script and have it run when starting cmd :-?

; Run a command when CMD.exe starts
[HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor]
"AutoRun"=-

EDIT: I just tried it. I have AutoRun=C:\mini\bashrc.cmd and bashrc.cmd is

@echo off
set TEST_VAR=something

when I start cmd and enter echo %TEST_VAR% it says something. So it works :)

11
  • 12
    Small snippet I use: doskey∙♦=exit (where is [char]4). Enables you to close the shell with Ctrl+D, Enter.
    – Joey
    May 23, 2010 at 12:13
  • 4
    HKEY_CURRENT_USER\Software\Microsoft\Command Processor with AutoRun=%HOMEPATH%\bashrc.bat So every user would have his own startup-sequence
    – bambam2174
    Jul 18, 2013 at 8:29
  • 9
    I must be an idiot but where do I put this code?
    – Marcel
    Oct 10, 2014 at 23:46
  • 1
    probably easiest to set to... AUTORUN=%USERPROFILE%\autorun.cmd so that it will use one for the current user... cool trick though, will try it when I get home... I mostly use bash lately, but this would help.
    – Tracker1
    Sep 18, 2015 at 22:40
  • 3
    @Tracker1 as cmd command without printing errors (i.e. when the script is missing): reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d @^%USERPROFILE^%\autorun.cmd" "2^>NUL /f Aug 11, 2022 at 11:47
25

Modification of nc3b's answer for those who are windows noobs like myself.

Make a file in C:\bashrc.cmd or wherever you want your "rc" file to reside.

@echo off
set TEST_VAR=something

Run regedit by searching for it or running it in cmd.

In the folders on the left navigate to:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor

Right click on the Command Processor folder and click New -> String Value. Enter the key AutoRun. Double click on AutoRun and enter the bashrc location C:\bashrc.cmd.

Test: Open a new cmd window and type:

echo %TEST_VAR%

if it says something, it works.

24

You can create a shortcut to cmd.exe and add the /k switch to run a certain .bat file on startup, something like this:

cmd.exe /k "%HOMEDRIVE%\%HOMEPATH%\cmd-startup.bat"

cmd-startup.bat in your home dir would then work just like .bashrc

1
8

Assuming your shell is Powershell, not cmd, you can create a profile for your shell. Such profiles basically are scripts which are executed when you start a shell. Just like .bashrc. An important part of this post might be the following:

The locations (on Windows Vista) of the profiles for the powershell.exe host are as follows:

  • %windir%\system32\Windows­PowerShell\v1.0\profile.ps1
    This is for all users of the computer and for all shells.
  • %windir%\system32\Windows­PowerShell\v1.0\Microsoft.Power­Shell_profile.ps1
    This is for all users of the computer, but it is only for the Microsoft.PowerShell shell.
  • %UserProfile%\Documents\Windows­PowerShell\profile.ps1
    This is for the current user only and all shells.
  • %UserProfile%\Documents\WindowsPowerShell\Micro­soft.PowerShell_profile.ps1
    This is for the current user only and only for the Microsoft.PowerShell shell.

These profiles aren't created by default. They exist only if you create them.

It seems to be the same on Windows 7, 8 and 10. You can also find more information here.


Note: You may need to run the following. Otherwise, your profile may not be executed.

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

You can also run the following as administrator to change the configuration for all users.

Set-ExecutionPolicy RemoteSigned
4

To all people coming from a unix-like world and landing on this question (i.e. everybody, since it asks about .bashrc): Radically improve your Windows console life and stop using stock cmd.exe and the default Windows terminal!! No one asking this question, or looking at answers to this question, should be using the current default cmd.exe and terminal!

If willing to switch off of stock cmd.exe (I know of no valid reason not to), one simple answer to the original question is:

Install and use Cmder and your .bashrc equivalent becomes %CMDER_ROOT%\config\user_profile.cmd, while also significantly improving your shell/console experience.

Opinions, extensions, and loosely related recommendations follow...


Cmder is a metapackage that packages up ConEmu, Clink, and a pile of extra settings and stuff. ConEmu is the terminal emulator that provides the startup script capability, which (through the default ConEmu settings Cmder provides) ends up routing to the indicated %CMDER_ROOT%\config\user_profile.cmd.

You may want to just explicitly use ConEmu and Clink independently, rather than use the bundled Cmder package.

To do that, and to add in some more "gotta work on windows, what should I do" bliss, below are some good/opinionated things to do. These are admittedly VERY loosely related to the initial question, but I've recently been through a Windows setup again, have typed up most of this already, and the target audience in this question is correct, so here you go...

  1. Use scoop.sh for all your installs
    • scoop is a great package manager for windows (similar to apt/yum/pacman/emerge/whatever)... where all packages install to the user (with no admin privileges needed)
    • definitely install the extras bucket for many non-console things (scoop bucket add extras)
    • you can also use chocolatey, but (my opinion) only if under duress and the thing you want is not in a scoop bucket
    • or just make your own scoop manifest
    • it is sad day when the thing I want isn't in a scoop bucket :(
    • unlike scoop, chocolatey needs admin privileges for its installs
  2. Use Clink as your shell instead of cmd.exe
    • scoop install clink
    • gives you GNU readline goodness (like ctrl-r, ctrl-s cmd history navigation) and more
    • be sure to also clink autorun install to make it run when cmd.exe is run
  3. Use ConEmu as your terminal emulator
    • scoop install conemu
    • the Windows terminal is terrible (but they are working on it)
    • be sure to configure it as the default terminal
      • "Force ConEmu" and "Register on OS startup" in Settings::Integration->Default term
    • SO MANY other configuration possibilities
      • if overwhelmed, maybe just stick with Cmder and its reasonable defaults (scoop install cmder), but I personally think it is cleaner to install/run Clink and ConEmu separately.
  4. Install Gow (like, right now)
    • scoop install gow
    • > 100 GNU tools (grep, vim, sed, ls, cat, curl, wc, less, find, etc, etc) you know and love, directly in your cmd.exe-like terminal without non-native weirdness
    • watch for name-collision renames like find->gfind and awk->gawk
    • I use Gow pretty much exclusively for basic Windows work instead of mucking with WSL, Cygwin, MSYS, git bash, etc.
    • Gow tools get as close to the GNU/*nix-on-Windows as I've experienced so far while staying native to Windows, without the headaches of Cygwin environments, WSL filesystem nastiness (which also being worked on), gitbash windows, etc
  5. Install other handy tools as you see fit (with scoop)
    • the main and extras scoop buckets have many more good tools and applications (python, openssh, ripgrep, fzf, sysinternals, sublime-text, meld, ccleaner, etc)
      • although definitely not a GNU/unix thing, sysinternals is an essential/classic collection of Windows native tools.
    • use scoop search and scoop info to find the ones you want, or browse the buckets (there are several buckets, not just main and extras)
2
3

EDIT: Found this: http://msdn.microsoft.com/en-us/library/bb613488(VS.85).aspx

To expound on Martin's solution (the shortcut), here's the PowerShell equivalent.

NOTE: You will need to have PowerShell script execution enabled. If this is disabled, you'll need to run an elevated PowerShell and execute Set-ExecutionPolicy Unrestricted. Obviously, this is not recommended for security reasons, but there ya go.

powershell.exe -noexit -File "%HOMEDRIVE%\%HOMEPATH%\ps-startup.ps1"

Have a file named ps-startup.ps1 in your user directory and your shortcut will execute it as a PowerShell file on startup. Just run PowerShell with this shortcut from here on out.

1
  • This can be achieved in a "better" way through powershell profiles. You can find the location of your current profile by: echo $profile . You can add lines to that file, and that should do it for future sessions. For existing sessions, you can reload the profile with: & $profile .
    – ashic
    Dec 16, 2015 at 10:54
3

Basically nc3b's answer works, but Chocolatey's DevBox-Common package will make it easier.

  1. If you have not already installed Chocolatey, install it.
  2. Install DevBox-Common package by choco install devbox-common -y
    • You should type this command inside administrative cmd.exe or PowerShell.
  3. Now you have .bashrc.bat and .bashrc.include.aliases-common.bat in your home path. Every file matching .bashrc.include.*.bat will be executed at every command shell start.
1

This answer can't be the replacement of whole feature of .bashrc but you can create alias in windows as easy as you could do in linux.

Create a directory lets say C:\commands, add this directory to Path Variable and inside the directory create a file cmdrc.bat.

Add the below codes to your cmdrc.bat file


set filename=%~1
set command=%~2

echo @echo off > C:\commands\%filename%.bat
echo %command% >> C:\commands\%filename%.bat

Now every time you have to create an alias like
alias ls=dir

you can run the command
cmdrc ls dir

every time you'll run this command it will create a new bat file in C:\commands directory.

1

you can set the registry key from commandline itself (and thus avoid the regedit part) with :

REG ADD "HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor" /v "Autorun" /t REG_SZ /d "%"USERPROFILE"%"\cmd-startup.bat
1
  • You mean HKEY_CURRENT_USER (which uses REG_EXPAND_SZ, not REG_SZ). What you're showing is the global autorun key, which you definitely don't want to be setting anything for. Mar 25, 2021 at 15:02
0

you can create a macros.doskey file I think or a macro.cmd something like that.

I made a repo that is garbage and has a lot of macros. I made a installation walkthrough, but it's too complicated

0

I just create some junk script, but it works. Just create .cmdrc in your user folder and if you start command prompt from code bellow, you run .cmdrc lines as commands.

cmd.exe /K "(echo @echo off & type %USERPROFILE%\.cmdrc & echo. & echo cls & echo @cmd.exe) > %tmp%\cmdrc.bat & cmd.exe /K "call %tmp%\cmdrc.bat""

And if you paste this into windows terminal, you could use this as default command instead of plain cmd.exe.

You must log in to answer this question.