If you work with a computer long enough, you’ll end up developing a few workflows. These can be simple or complex, but it’s likely you’ll end up with a few that are unique to you. The more you do with your computer, the more likely you’ll end up using the Command Prompt or the more powerful PowerShell.
Sometimes you might find yourself running the same few commands every time you launch PowerShell or the command prompt. Instead, why not save some time and run them automatically on launch?
What Can You Do with Automatic Commands?
You may have a workflow that sees you creating a bunch of files in a directory each time you use Command Prompt or PowerShell. With automatic commands you can clean that directory each time you open a prompt. You can also use this to change the default directory the Command Prompt or PowerShell opens in.
Those are just a few examples. Your own workflows will dictate what you automatically run.
Setting Up Automatic PowerShell Commands
To run commands automatically in PowerShell, edit your profile. To get started, check to see if you have a profile already. Open PowerShell and type the following:
This will return either True or False. If it’s false, run the following command:
This will overwrite any existing profile. If the previous command returned True, you likely don’t want to run this. If you want to start over, go right ahead.
To actually set up your automatic commands, edit your profile. Use the text editor of your choice, but Notepad is installed by default. Edit the file in Notepad by typing the following:
Put any commands you can run in PowerShell here, and they’ll automatically run every time you open it. If you’re familiar with Linux, this is essentially the same as editing your “~/.bash_profile” file.
In most cases PowerShell’s execution policy will prevent this script from running. This is to help keep your system safe. To allow your the script to run, launch PowerShell as administrator and run the following:
Read over the prompt and enter Y to set the new execution policy.
Setting Up Automatic Command Prompt Commands
Compared to the Unix-style approach you use to automatically run commands in PowerShell, the Command Prompt is much more Windows-like. There are two different ways to do this, depending on how you prefer to work.
Method 1: The Windows Registry
Like the PowerShell method above, this specifies a script to run every time that you launch the Command Prompt. Unlike that method, it uses the Windows Registry to define which file runs. For this example we’re assuming you want to use a file called “auto.cmd.”
To create the proper registry value, open the Command Prompt and run the following:
Now create a file in your profile folder (usually “C:\Users\USERNAME”) named “auto.cmd.”
Edit this file with the commands you want to run automatically when the Command Prompt is launched.
If you decide you no longer need this to run automatically, delete the registry key. Just run the following:
Method 2: Use a Shortcut
If you have a simple case and don’t want to use the registry, you can use a desktop shortcut. As an example, in Windows 10 you can find Command Prompt in the Start menu, right click and select “Open File Location”. Copy the Command Prompt shortcut here and paste it to your desktop.
Right-click on your newly pasted shortcut and select Properties. You’ll see a section with the app’s path. It will read like the following:
Simply alter this by adding
-cmd /K and then the command or commands of your choosing. Here’s an example:
This will simply open the Command Prompt and then clear the screen. You can also chain commands by using
&& between them. Here’s another example:
This will clear the screen and then display the contents of the directory.
Depending on what you need to do, one or more of the above methods should work for you. If you end up using the Command Prompt even more, take a look at our guide to customizing how it looks.