Powershell function to rename a computer

This is something I took from one of my larger deployment scripts and found it to be very useful and reusable.
Just to give you a use case: In my situation the parameter $NewComputerName is calculated based on the device type (server, laptop or desktop), the IP range and a random number taken from a text file located on a file share.