Mar 24, 2015

Windows Command Prompt Redux

Just over two years ago, I wrote about my command line setup for Windows. In that time, I've streamlined the setup process a bit thanks to Chocolatey, and made some improvements to the handling of SSH keys. So I figured it was time to write about it again.

Below are step by step instructions, feel free to pick and choose what works for you. If you want something preconfigured, maybe Cmder will be a better option for you.

Chocolatey

Chocolatey makes installing Windows software quick and easy. You can find installation instructions at chocolatey.org.

Make sure you're using version 9.9 or later. To check, run:

choco -v

If you're using an older version, I recommend upgrading:

choco update -Pre

Note: All Chocolatey commands should be run in an administrative Powershell or Cmd command prompt.

ConEmu

ConEmu is a console replacement. It acts as a host for console tools such as Cmd and Powershell. There are a few other similar tools, but this one has tons of options so you can configure it exactly to your liking. Install it with the following command:

choco install conemu -y

After you start ConEmu, take some time to look through the settings. There is plenty in there to configure. For example, one thing I like to do is to pin ConEmu to the Windows task bar then in the settings. Under Main/Task Bar, check Add ConEmu tasks to taskbar and then hit the Update Now! button:

ConEmu taskbar configuration

Another useful setting is to set the PowerShell Admin prompt as the default task. Go to Startup, pick "Specified named task" and pick the PowerShell Admin task. I use PowerShell because of posh-git, as we'll see below.

Git

The next important tool is Git. Even if you don't use it for source control, it also comes with other very useful tools such as a SSH agent and a SSH client. Here's the installation command:

choco install git.install -y --params="/GitAndUnixToolsOnPath"

The extra parameter ensures all git tools are in the PATH instead of just Git.

posh-git

Posh-git is a set of powershell scripts that make working with git easier. First by showing all kinds of information in your git repository in a concise manner on the command prompt. But also because it handles the business of starting Git's SSH agent. I ended up forking it in order to allow it to provide all of your SSH keys to the agent, and to make the agent available to all other processes in the system.

Because of this, installation is a little be more involving. First clone the repository in a folder of your choice:

git clone https://github.com/Ventajou/posh-git.git -b ssh_agent_updates

If you haven't already, ensure the PowerShell execution policy is set:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm

Then install it:

cd posh-git
.\install.ps1
. $PROFILE

you may see an error about the .ssh folder missing. You can add it manually:

cd ~
mkdir .ssh

Posh-git will automatically start the SSH agent and load all keys from your ~/.ssh folder. This is useful for example if you use several Git accounts (like both GitHub and BitBucket) or if you want to SSH into a Linux system. The agent will be started the first time you start a PowerShell session and it will remain running until you shut down your computer.

Visual Studio Command Prompt

Visual Studio comes with a preconfigured command prompt shortcut that lets you do things like run MSBuild. But it's based on Cmd, so it's either MSBuild or posh-git. That can be fixed by adding the following to your PowerShell profile:

#Set environment variables for Visual Studio Command Prompt
pushd 'c:\Program Files (x86)\Microsoft Visual Studio 12.0\VC'
cmd /c "vcvarsall.bat&set" |
foreach {
  if ($_ -match "=") {
    $v = $_.split("="); set-item -force -path "ENV:\$($v[0])"  -value "$($v[1])"
  }
}
popd
write-host "`nVisual Studio 2010 Command Prompt variables set." -ForegroundColor Yellow

This was slightly adapted from this StackOverflow answer. Your PowerShell profile is typically in C:\Users\<username>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1.

Take it further

If you've followed this far, your setup is now just like mine. I'm sure you also have tricks of your own to be more productive with Windows development. Feel free to share them in the comments!

Comments powered by Disqus