Powershell script tips

Write to a file

Use Set-Content and Add-Content

Set-Content "C:\Program Files\Sublime Text 2\subl.bat" "@echo off"
Add-Content "C:\Program Files\Sublime Text 2\subl.bat" "sublime_text.exe %1"

Adding to environmental path variable

from http://softwaresalariman.blogspot.co.uk/2008/01/add-path-environment-variable-in.html

Set-Content "C:\Program Files\Sublime Text 2\subl.bat" "@echo off"
Add-Content "C:\Program Files\Sublime Text 2\subl.bat" "sublime_text.exe %1"
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\Program Files\Sublime Text 2", "Machine")

windows batch file tips

using batch parameter

@echo off
sublime_text.exe %1

hide output and output only the stuff you want to reveal

from http://stackoverflow.com/questions/2044882/how-to-hide-batch-output

@echo off
echo verbose stuff 1
echo verbose stuff 2
echo verbose stuff 3
echo important stuff! >&2
echo verbose stuff 4

launch an exe file asynchronously

By default, scripts run synchronously. What it means is that when you launch an exe file, the batch file doesn’t go on running until you close the program, and it is quite annoying.

So, use “start” command to run steps asynchrounously.

start sublime_text.exe %1

git tips

I have used git for a year now on my personal projects, but it’s still mysterious. I feel like I need a good gui like TortoiseHG. Probably Git Extension is the most mature project in guifying git.

Setting up your git environment

Set up can be a times-taking and boring job, but well, you can skip it. It’s something you need to to properly, to make the whole experience pleasant!

An augmented .gitignore

This will ignore rails document, vim, emacs swap files, and Mac OS X Finder’s DS_Store.

# Ignore bundler config

# Ignore the default SQLite database.

# Ignore all logfiles and tempfiles.

# Ignore other unneeded files.

Configure git to use BeyondCompare


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


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

To launch a diff with BC3, use the command “git difftool” or “git difftool foofile.txt”

Line endings

If you use git on windows like me, it’s better to configure it to “commit as-is, checkout as-is”.

git config --global core.autocrlf false

Pull behaviour

By default Git will merge remote changes with your local changes when pulling. This can result in an ugly commit graph that makes it difficult to track changes. Rebasing on the other hand results in a single commit line graph. Rebasing long-lived branches is not recommended.

The guidance is that when pulling; when you’re only making changes on the master branch (or any long-lived remote branch) always use rebase. If you’re working on a long-lived branch merge should be used when pulling in changes from the original branch or merging back to that branch.

git pull --rebase


Generate a new ssh key on your machine. Go for default file name and type in your password.

$ ssh-keygen -t rsa -C "your_email@youremail.com"

Then copy the key from id_rsa.pub and register it on your github.com.

Create an app first on github. Push your changes to it.

$ git remote add origin git@github.com:/first_app.git
$ git push -u origin master

Sometimes, you make a mistake while creating a remote origin. Then

$ git remote rm origin

Rename remote

$ git remote -v
appharbor       https://andrewchaa@appharbor.com/londonubf.git (fetch)
appharbor       https://andrewchaa@appharbor.com/londonubf.git (push)
origin  git@github.com:andrewchaa/LondonUbf.git (fetch)
origin  git@github.com:andrewchaa/LondonUbf.git (push)

$ git remote rename appharbor apphb

$ git remote -v
apphb   https://andrewchaa@appharbor.com/londonubf.git (fetch)
apphb   https://andrewchaa@appharbor.com/londonubf.git (push)
origin  git@github.com:andrewchaa/LondonUbf.git (fetch)
origin  git@github.com:andrewchaa/LondonUbf.git (push)

staging files

$ git add . // This will stage all files
$ git add -u // This will stage delete files
you put the period at the end.

Discard unstaged files

from http://stackoverflow.com/a/52713/437961

$ git checkout path/to/file/to/revert // for a specific file
$ git checkout -- . //This will delete all unstaged files. Make sure you put the period at the end.

Branch, edit, and merge

To edit readme file, you can checkout in to a new branch. It’s kind of a good practice that you protect the main branch by branching your change out. You can delete the temp branch afterwards.

git checkout -b modify-README
git mv README.rdoc README.md
mate README.md

it commit -a -m 'improve the README file'
git branch -d modify-README

-a means commit all files that you changed.
if you use -D, instead of -d, it will delete the branch even though you haven’t merged in the changes.

Creating a branch

git branch NuGetUpdate
git checkout NuGetUpdate

or this is one liner.

git checkout -b NuGetUpdate

Creating a remote branch

The title is a bit misleading. You just create a local branch and push it remotely, then the remote branch is created.

push <remote-name> <branch-name> // syntax

git branch 1.0-dev
git checkout 1.0-dev
git push origin 1.0-dev

Caching your credential

If you have to type in your username and password to push your change to github, your fingres and wrists will suffer. You can cache your credential for a certain period.

I cached it for 5 hours. If you want an hour, use 3600.

git config --global credential.helper 'cache --timeout=18000'

ignore ssl certificate error

when you commit to your company githup repository that doesn’t have signed certificate, unfortunately, you get an error. You can get around it by turning off the warning.

git config – –global http.sslVerify false

Git commit against tag with no branch

I honestly panicked. I wasn’t aware that the working directory was attached to any branch. I added and committed my changes (which took an hour of toil). Then it wasn’t in any branch. I checked out to master. All the changes seemed to be lost!

The saviour was git reflog. You can find your commit SHA1, and checkout with it.

D:\dev\files>git reflog
WARNING: terminal is not fully functional
ce0320e HEAD@{0}: checkout: moving from tempbranch to NuGetUpdate
ce0320e HEAD@{1}: checkout: moving from NuGetUpdate to tempbranch
ce0320e HEAD@{2}: merge tempbranch: Fast-forward
1b355c7 HEAD@{3}: checkout: moving from tempbranch to NuGetUpdate
ce0320e HEAD@{10}: commit: updated references with the downgraded framework

Then you can check it out, create a new branch, and merge the branch to master or whatever, phew!

$ git checkout ce0320e
$ git checkout -b <new_branch_name>
$ git checkout master
$ git merge new branch
$ git branch -d newbranch
$ git push ...

Regular expression tips

There are several regular expression I use more often than I thought. This will be a comprehensive list of those expressions.

Get file extension

Useful if you need to replace file extension manually as I did with Xceed component


Check if the input string is UK Post code


  • Clean up special characters that can break javascript function parameter
  • _cleanupRegEx = new Regex(@"[\x00-\x09\x0B\x0C\x0E-\x1F\x27\x7F\x5C]|<[^>]*>|\s+", RegexOptions.Compiled)

    Those hx codes are a bit unfriendly. Please use the below table for your reference.

    Named group

    var titleRegex = new Regex("<title>(?<title>.*?)</title>", RegexOptions.IgnoreCase);
    var contentRegex = new Regex("<content>(?<content>.*?)</content>", RegexOptions.IgnoreCase);
    var page = new DataPage();
    var titleMatch = titleRegex.Match(content);
    page.Title = titleMatch.Groups["title"].Value;
    var contentMatch = contentRegex.Match(content);
    page.Content = contentMatch.Groups["content"].Value;