My windows development environment in 2016

Every day, we as developers, use a wide range of tools to perform our tasks. Re-installing your machine and getting everything you need is always a bit of a search. So I have created this blog posts to store how I install my development machine.

Installing Windows Powershell version 5 (only on < windows 10)

Download the latest version of Powershell (not required on Windows 10, we use version 5)
Go to the following link Windows Management Framework 5.0 and download Win8.1AndW2K12R2-KB3134758-x64.msu.

Once installed verify your PowerShell version by performing the following command:

$PSVersionTable.PSVersion

This should display a Major version of 5.

PS C:\> $PSVersionTable.PSVersion  
Major  Minor  Build  Revision  
-----  -----  -----  --------
5      1      14393  187  

Setup powershell version 5

By default the Powershell console is set to Restricted to make it as secure as possible if you don't need it. Since we are going to use it, set it to RemoteSigned.

Start Powershell in Administration mode and change the execution policy to RemoteSigned

Set-ExecutionPolicy RemoteSigned  

There are a few different policies you can pick here:

  • Restricted: Does not load configuration files or run scripts. "Restricted" is the default execution policy.
  • AllSigned: Requires that all scripts and configuration files be signed by a trusted publisher, including scripts that you write on the local computer.
  • RemoteSigned: Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.
  • Unrestricted: Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.
  • Bypass: Nothing is blocked and there are no warnings or prompts.

Unblock script files

If you download Powershell scripts on-line and place them on your pc you first need to unblock them otherwise you are unable to execute them. You can do that by performing the following command:

Unblock-File script.ps1  

Or if you want to unblock a complete directory:

gci | Unblock-File  

Or in the properties windows of the file by ticking the unblock checkbox.

unblock security of file

You should be able to exit the Administration mode Powershell now, and start it up normally again.

Creating PowerShell default profile

A PowerShell profile is used to run certain commands at the startup of PowerShell, which allow you to modify your environment.

Let's create a Powershell profile.

New-Item -Path (Split-Path -parent $profile) -ItemType Directory  
touch $profile  

This will create an empty file named Microsoft.PowerShell_profile.ps1 in the folder WindowsPowerShell in your My Documents folder.

Open up the file in your default text editor

start $profile  

And add the following content to the file:

Write-Host "Hello, world from the default profile"  

Restart PowerShell and if everything worked you should be able to see:

It's working :-)

Now create the following folders on your disk:

md Tools  
md Repository  

They can be placed anywhere on your disk, as long as you remember for now where they are, I placed them on C:\ (so everywhere where I use the path C:\Repository or C:\Tools be sure to replace them with your location).

And add a folder inside the WindowsPowerShell folder in your Documents folder with the name Modules. We will store modules here which we will use later on.

Open up your Microsoft.PowerShell_profile.ps1 (type start $profile) file again and replace the Write-Host "Hello, world.. with the following text:

##-------------------------------------------
## Create the default drives to common paths
##-------------------------------------------
New-PSDrive -name Repository -psprovider FileSystem -root C:\Repository | Out-Null  
New-PSDrive -name Tools -psprovider FileSystem -root C:\Tools | Out-Null  
New-PSDrive -name Modules -psprovider FileSystem -root $Home\Documents\WindowsPowerShell\Modules | Out-Null

##-------------------------------------------
## Goto repository folder
##-------------------------------------------
Set-Location Repository:  

Save the file and restart the PowerShell prompt. You can now see that your tools and repository folder became drives.

Create a folder named PowerShellScripts in your Tools folder

cd Tools:\  
md PowerShellScripts  

Inside the PowerShellScripts folder create a file named Add-Path.ps1 and add the following content to it:

Open up your Microsoft.PowerShell_profile.ps1 (type start $profile) and add the following above the goto repository section:

##-------------------------------------------
## Load default script libraries
##-------------------------------------------
Get-ChildItem -Path "Tools:\PowerShellScripts\" -Recurse -Filter *.ps1 | ForEach-Object {. $_.FullName} | Out-Null  

Restart PowerShell. It should now try to load all the scripts it finds in the PowerShellScripts folder and make them available by default. You can test this by performing the following commands:

Add-Path C:\Tools  
$env:PATH

This should now display ;C:\Tools at the end of the path string. This is only available during the time the session is open, so when you close PowerShell again it will be lost.

Installing git

Download the latest version of PortableGit from https://github.com/git-for-windows/git/releases and unpack it to the C:\Tools folder. I like to give it the name of the version of git I downloaded (PortableGit-2.10.0-64-bit) so I can easily see what version is installed.

Open up your Microsoft.PowerShell_profile.ps1 (type start $profile) and add the following above the goto repository folder section:

##-------------------------------------------
## Add paths to PATH
##-------------------------------------------
Add-Path    (Get-Item "Tools:\PortableGit-2.10.0-64-bit\usr\bin").FullName,  
            (Get-Item "Tools:\PortableGit-2.10.0-64-bit\cmd").FullName

Save it and restart PowerShell. You should now be able to run the git command:

Configure Git

Setup your user information for Git, this information is used when you perform commits.

git config --global user.name "Your name"
git config --global user.email your@email.com

Create a default .gitignore file

Sometimes there are files that you always want to ignore, when checking into git. Also in the case where you did not yet, or forget to add them in the .gitignore file of your repository. We will not create a file to store them and set it up.

cd $env:USERPROFILE  
touch .gitignore.  
git config --global core.excludesfile $env:USERPROFILE/.gitignore  

You can now edit the file .gitignore that is stored in your user profile folder to add globally ignored files.

Set the default push mode to simple

The default in Git 2.0 is simple.

git config --global push.default simple  

Modify the default pager from less to cat

We as windows users are not that used to the less pager (It's that situation where you don't have a clue how to exit this colon thing, see image below), so we can just turn it off and use powershell for paging.
less __pager__ Perform the following command

git config --global core.pager cat  

Change the colors used

By default git uses a color that is hard to read on the blue PowerShell color:
red color in git is hard to read We can fix this by running the following commands (this can also be done by editing your .gitconfig file):

git config --global color.ui true  
git config --global color.status.changed "red bold"  
git config --global color.status.untracked "red bold"  
git config --global color.status.added "green bold"  

git config --global color.diff.meta "yellow bold"  
git config --global color.diff.frag "magenta bold"  
git config --global color.diff.old "red bold"  
git config --global color.diff.new "green bold"

git config --global color.branch.current "cyan bold"  
git config --global color.branch.local "yellow"  
git config --global color.branch.remote "green bold"  
git config --global color.branch.upstream "magenta bold"  

Now the same screen will look like:
git colors fixed

Modify the way the git log output looks like

By default the result of the command git log will look like:
default git log This is almost never the format I want to see it in, so I've modified the default with the following commands:

git config --global format.pretty "tformat:%C(yellow bold)%h %C(green bold)%>>|(22)%ad %C(white bold)%>>|(42)%aN %C(dim white)%s %C(yellow bold)%d^I"  
git config --global log.date relative  

Now it will look like:
modified git log

You can now perform the following command to get the last 7 (or any other number of) commits:

git log -7  

Creating aliases for common tasks

There are some commands that you would probaly use a lot, it would be easier to have short aliases for them. Perform the following commands to create them:

git config --global alias.dt difftool  
git config --global alias.mt mergetool  
git config --global alias.st status  

You will now be able to type git st instead of git status.

Creating an alias to show the log as tree

Perform the following command to create an alias to see the log as a tree

git config --global alias.logtree "log --graph --abbrev-commit --decorate --format=tformat:'%C(bold yellow)%h %C(bold green)%ar %C(white bold)%aN %C(dim white)%s%C(bold yellow)%d%C(reset)' --all"  

You can now type git logtree:
git logtree

Creating aliases to see incoming and outgoing commits

git config --global alias.outgoing "log @{u}.. --oneline"  
git comfig --global alias.incoming "!git remote update -p; git log ..@{u} --oneline"  

git outgoing

Creating an alias to see difference between branches

git config --global alias.branchdiff "!f() { git diff --stat --color $1..$2; }; f"  

git branchdiff

Set the default text editor to use

Download and install Visual Studio Code and set it as the default git text editor.

git config --global core.editor "code --wait"  

Line endings

Set git to convert LF (Mac/ Linux) line endings into CRLF (Windows), this is your default or fallback value. This will be used if you don't specify it in your .gitattributes file.

git config --global core.autocrlf true  

Set the tool to use for performing diff

Download and install a copy of Beyond Compare from Scooter Software download page

Setup beyond compare as git diff tool

git config --global diff.tool bc3  
git config --global difftool.bc3.path "C:\Program Files (x86)\Beyond Compare 3/bcomp.exe"  
git config --global difftool.prompt false  

If you bought the Pro Edition then you can also setup 3 way merge

git config --global merge.tool bc3  
git config --global mergetool.bc3.path "C:\Program Files (x86)\Beyond Compare 3/bcomp.exe"  

Extending the command prompt to show more information.

Posh-Git is a PowerShell extension to show Git information.

We can get it directly from github.com, by performing the following commands:

cd Modules:\  
git clone https://github.com/dahlbyk/posh-git.git  

We now have a folder called posh-git in our Modules folder.
Open up the Microsoft.PowerShell_profile.ps1 (type start $profile) file from the WindowsPowerShell folder in your Documents folder again and add the following above the goto repository folder section

##-------------------------------------------
## Setup posh-git
##-------------------------------------------
Push-Location (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)  
Import-Module posh-git

#setup prompt
function global:prompt {  
    [Console]::ResetColor()
    $realLASTEXITCODE = $LASTEXITCODE
    $Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultForegroundColor
    Write-Host($pwd.Path) -nonewline
    Write-VcsStatus
    $global:LASTEXITCODE = $realLASTEXITCODE
    return "> "
}

Pop-Location  

Restart the PowerShell and goto the Modules folder and into the posh-git folder. The prompt should now display the git branch you are on and when there are changes the amount of files checked out, etc. We also modified the prompt a little bit to remove the PS part of it.

Modify the change directory cd command

In PowerShell the cd command actually is an alias, which can be redirected to another function.
Which comes in handy to make checking out a branch easier, we will wrap the default function with a function that checks if the folder/ directory starts with a :. If that's the case, we perform a git checkout, otherwise we perform the default behavior.

Save the following file in Tools:\PowerShellScripts as Set-Location.ps1 and restart the PowerShell console.

function Set-GitLocation($cmd)  
{
    if ($cmd.StartsWith(":")) 
    {
        $cmd = $cmd.Replace(':','')
        git checkout $cmd
    }
    else 
    {
        Set-Location $cmd
    }
}

Remove-Item alias:cd  
Set-Alias cd Set-GitLocation  

If you are in a repository now, type:

cd :develop  

to switch to the develop branch (if it exists, type git branch --list to get a list of branches, type git branch name to create a branch for testing if none exist and git branch -d name to delete it again).

Installing SSH certificate

We will install a SSH certificate so we don't need to provide our username and password on each push to a remote repository (for example GitHub).

Perform the following command in the PowerShell command prompt:

ssh-keygen -t rsa -C "your@email.com"  

Press enter to pick the default path to store the ssh key pair. Enter a pass phrase, this phrase is used to allow you to use the certificate and you will need to provide it when starting PowerShell if they SSHAgent isn't running yet.

It will generate a key fingerprint and key's randomart image (both masked with red below) you don't need them for now.

Open up your Microsoft.PowerShell_profile.ps1 (type start $profile) and add the following above the goto repository folder section

##-------------------------------------------
## Start SSH Agent
##-------------------------------------------
Start-SSHAgent  

Restart PowerShell and it will now ask once for the pass phrase, if the SSH agent is already running it will skip this step.

Adding the SSH key to GitHub

Copy the public key to the clipboard by performing the following command in PowerShell

cat $Home/.ssh/id_rsa.pub | clip.exe  
  • Go to your GitHub settings page
  • Click in the left menu on SSH keys
  • Press the button 'Add SSH key'
  • Provide a name for your key so you know what it is
  • And paste the public key inside the key textbox
  • Press add key

To clone a repository with SSH be sure to use the correct url, for example:

git clone git@github.com:eriklieben/DefinitelyTyped.git  

And not

git clone https://github.com/eriklieben/DefinitelyTyped.git  

Because this will still require a username and password (when pushing to the repository).

Adding aliases for common application

Aliases are handy shortcuts to launch your favourite program. You can make as many as you can remember quickly.

Navigate to your PowershellScripts folder in the tools folder and create a file named Find-And-Set-Alias.ps1. And add the following content to it:

And the aliases you need to the Microsoft.PowerShell_profile.ps1 (type start $profile) file found in your Documents\WindowsPowerShell

##-------------------------------------------
## Add alias
##-------------------------------------------
Find-To-Set-Alias "c:\program files*\Microsoft Visual Studio 14.0\Common7\IDE" devenv.exe vs  
Find-To-Set-Alias "c:\windows\system32\WindowsPowerShell\v1.0\" PowerShell_ISE.exe psise  

Save and restart PowerShell. You should now be able to use your alias, type for example vs to test it.

PowerShell command to start Visual Studio

The alias is nice, but for starting Visual Studio you now still need to type vs solution.sln. We are now going to create an function to open the solution file even quicker. In your PowerShellScripts folder add a file named VisualStudio-Here.ps1 and add the following content: