Rename Database By closing connections

When trying to rename database with open database connections its always a pain as the admin has to kill those connections. Instead of going that route one can use the following code.

USE [master]
GO
ALTER DATABASE NameOfYourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE NameOfYourDatabase
Modify Name = NewNameOfYourDatabase
GO
ALTER DATABASE NewNameOfYourDatabase SET MULTI_USER;

Note: Name of the database in the query above is case sensitive.