*Update*

*********************************************************************************************************************************************

Reuben Welsh came up with a great feature request regarding verifying the checksum of the files that’s being installed.
This has been implemented in the version 2.1 of the script.

*********************************************************************************************************************************************

For a year ago I was publishing a script that helped with installing prerequsites for Exchange 2013, this has now been updated so it also works for Exchange 2013 SP1 and Windows Server 2012 R2.
I hope this script helps someone for a smoother installation.
The script can be downloaded here: http://www.testlabs.se/blog/wp-content/uploads/2014/04/Install-Exchange2013PreReqs_v2.1.zip

*********************************************************************************************************************************************

Feel free to use this, make sure to test it in a test environment before using it inside production.
All contents is provided “AS IS” with no warranties, and confers no rights. You assume all risk for your use.

# +=======================================================================
 # | Blog: <a href="http://www.testlabs.se/blog">http://www.testlabs.se/blog</a>
 # | Twitter: @jonand82
 # | =============================================
 # | Filename: Install-Exchange2013PreReqs_v2.1.ps1
 # |
 # | CREATED BY: Jonas Andersson - Original written by: Pat Richard, Anderson Patricio and Bhargav Shukla
 # | FUNCTION: Installs and configures the prerequisites to install Exchange 2013 on..
 # | ..Windows Server 2008 R2, Windows Server 2012 or Windows Server 2012 R2 server
 # |
 # | CHANGE LOG:
 # | v1.0 - 2013-04-05, *Created*
 # | v2.0 - 2014-04-11, *Updated with functions*
 # | v2.1 - 2014-04-28, *Checksum implemented*
 # |
 # | <a href="http://www.ucblogs.net/blogs/exchange/archive/2009/12/12/Automated-prerequisite-installation-via-PowerShell-for-Exchange-Server-2010-on-Windows-Server-2008-R2.aspx">http://www.ucblogs.net/blogs/exchange/archive/2009/12/12/Automated-prerequisite-installation-via-PowerShell-for-Exchange-Server-2010-on-Windows-Server-2008-R2.aspx</a>
 # | <a href="http://msmvps.com/blogs/andersonpatricio/archive/2009/11/13/installing-exchange-server-2010-pre-requisites-on-windows-server-2008-r2.aspx">http://msmvps.com/blogs/andersonpatricio/archive/2009/11/13/installing-exchange-server-2010-pre-requisites-on-windows-server-2008-r2.aspx</a>
 # | <a href="http://www.bhargavs.com/index.php/powershell/2009/11/script-to-install-exchange-2010-pre-requisites-for-windows-server-2008-r2/">http://www.bhargavs.com/index.php/powershell/2009/11/script-to-install-exchange-2010-pre-requisites-for-windows-server-2008-r2/</a>
 # | <a href="http://www.tinyint.com/index.php/2011/09/14/get-an-md5-or-sha1-checksum-with-powershell/">http://www.tinyint.com/index.php/2011/09/14/get-an-md5-or-sha1-checksum-with-powershell/</a>
 # +=======================================================================

# Detect correct OS here and exit if no match
 if (-not((Get-WMIObject win32_OperatingSystem).OSArchitecture -eq '64-bit') -and (((Get-WMIObject win32_OperatingSystem).Version -eq "6.1.7601") -or ((Get-WMIObject win32_OperatingSystem).Version -eq "6.2.9200") -or ((Get-WMIObject win32_OperatingSystem).Version -eq "6.3.9600"))) {
 Write-Host "This script requires a 64bit version of Windows Server 2008 R2, Windows Server 2012 or Windows Server 2012 R2, which this is not." -ForegroundColor Red -BackgroundColor Black
 Exit
 }

function Get-Checksum
 {
 Param (
 [string]$File=$(throw("You must specify a filename to get the checksum of.")),
 [ValidateSet("sha1","md5")]
 [string]$Algorithm="sha1"
 )

$fs = new-object System.IO.FileStream $File, "Open"
 $algo = [type]"System.Security.Cryptography.$Algorithm"
 $crypto = $algo::Create()
 $hash = [BitConverter]::ToString($crypto.ComputeHash($fs)).Replace("-", "")
 $fs.Close()
 $hash
 }

function Disable-UAC() {

function UAC-status() {

$path = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System"
 $EnableLUA = ""
 $EnableLUA = Get-ItemProperty $path -Name EnableLUA

if ($EnableLUA.EnableLUA -eq "0") {
 [bool]$UACenabled = $false
 }

if ($EnableLUA.EnableLUA -eq "1") {
 [bool]$UACenabled = $true
 }

return $UACenabled
 }

[bool]$status = UAC-status

if ($status -ne $true) {
 Write-Host "UAC already disabled" -ForegroundColor Green
 return
 }

else {

Write-Host "Starting to disable UAC.."

Set-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System" -Name "EnableLUA" -Value 0
 Start-Sleep -Seconds 2

[bool]$status = UAC-status

if ($status -ne $false) {
 Write-Host "An error occurred, try again." -ForegroundColor Red
 return
 }

else {
 Write-Host "UAC Enabled:", $status
 Write-Host "...."
 Write-Host "UAC is now disabled" -ForegroundColor Green
 Write-Host "Registry key HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA has been changed." -ForegroundColor White
 }
 }
 }

function Disable-FW() {

function FW-status() {

$status = ""
 $status = netsh advfirewall show allprofiles state

if ($status | Select-String "ON") {
 [bool]$enabled = $true
 }

else {
 [bool]$enabled = $false
 }

return $enabled
 }

[bool]$fwstatus = FW-status

if ($fwstatus -eq $false) {
 Write-Host "Firewall is already disabled" -ForegroundColor Green
 return
 }

if ($fwstatus -eq $true) {

Write-Host "Starting to disable the firewall.."

$param = "advfirewall set allprofiles state off"
 $run = (Start-Process netsh -ArgumentList $param -Wait -PassThru).ExitCode
 if ($run -ne 0) { Write-Host "Failed!" -ForegroundColor Red }

[bool]$fwstatus = FW-status

if ($fwstatus -ne $false) {
 Write-Host "An error occurred, try again." -ForegroundColor Red
 return
 }

else {
 Write-Host "Firewall enabled:", $fwstatus
 Write-Host "...."
 Write-Host "Firewall is now disabled" -ForegroundColor Green
 }
 }
 }

function InstallFilterPack() {

function DownloadFilterPack1() {

#Download Microsoft Filter Pack
 Write-Host "Downloading Microsoft Filter Pack..." -nonewline
 $clnt = New-Object System.Net.WebClient
 $url = "<a href="http://download.microsoft.com/download/0/A/2/0A28BBFA-CBFA-4C03-A739-30CCA5E21659/FilterPack64bit.exe&quot;">http://download.microsoft.com/download/0/A/2/0A28BBFA-CBFA-4C03-A739-30CCA5E21659/FilterPack64bit.exe"</a>
 $clnt.DownloadFile($url,$file1)
 Write-Host "done!" -ForegroundColor Green
 }

function DownloadFilterPack2() {

#Download Microsoft Filter Pack SP1
 Write-Host "Downloading Microsoft Filter Pack SP1..." -nonewline
 $clnt = New-Object System.Net.WebClient
 $url = "<a href="http://download.microsoft.com/download/A/A/3/AA345161-18B8-45AE-8DC8-DA6387264CB9/filterpack2010sp1-kb2460041-x64-fullfile-en-us.exe&quot;">http://download.microsoft.com/download/A/A/3/AA345161-18B8-45AE-8DC8-DA6387264CB9/filterpack2010sp1-kb2460041-x64-fullfile-en-us.exe"</a>
 $clnt.DownloadFile($url,$file2)
 Write-Host "done!" -ForegroundColor Green
 }

function InstallFilterPack1() {

#Install Microsoft Filter Packs 1
 Write-Host "Installing Microsoft Filter Packs..."

$args = "/quiet /norestart"
 $setup1 = (Start-Process $file1 -ArgumentList $args -Wait -PassThru).ExitCode
 if ($setup1 -eq 0) {
 Write-Host "Successfully installed $file1" -ForegroundColor Green
 }

if ($setup1 -ne 0) {
 Write-Host "Failed!" -ForegroundColor Red
 }
 }

function InstallFilterPack2() {

$args = "/quiet /norestart"
 $setup2 = (Start-Process $file2 -ArgumentList $args -Wait -PassThru).ExitCode
 if ($setup2 -eq 0) {
 Write-Host "Successfully installed $file2" -ForegroundColor Green
 }

if ($setup2 -ne 0) {
 Write-Host "Failed!" -ForegroundColor Red
 }
 }

if (Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\{95140000-2000-0409-1000-0000000FF1CE}" -ErrorAction SilentlyContinue) {

Write-Host "FilterPack is already installed." -ForegroundColor Yellow
 return
 }

else {

trap {
 Write-Host "Problem downloading FilterPackx64.exe. Please visit: <a href="http://www.microsoft.com/en-us/download/details.aspx?id=26604">http://www.microsoft.com/en-us/download/details.aspx?id=26604</a> and <a href="http://www.microsoft.com/en-us/download/details.aspx?id=17062&quot;">http://www.microsoft.com/en-us/download/details.aspx?id=17062"</a>
 break
 }

#set a var for the folder you are looking for
 $folderPath = 'C:\Temp'

#Check if folder exists, if not, create it
 if (Test-Path $folderpath) {
 Write-Host "The folder $folderPath exists."
 }

else {
 Write-Host "The folder $folderPath does not exist, creating..." -NoNewline
 New-Item $folderpath -type directory | Out-Null
 Write-Host "done!" -ForegroundColor Green
 }

$file1 = $folderPath+"\FilterPack64bit.exe"
 $file1chksum = "9253B09EA9A2ADD0E773CD30FE4120F5732C4D98"

$file2 = $folderPath+"\filterpack2010sp1-kb2460041-x64-fullfile-en-us.exe"
 $file2chksum = "2C7B81D0C148E3E1894A9183FEE6A543EA5DC9B2"

# Check if file exists, if not, download it
 if (Test-Path $file1) {
 Write-Host "The file $file1 exists."

$chksum1 = Get-Checksum $file1
 if ($file1chksum.CompareTo($chksum1) -eq 0) {
 Write-Host "Checksum OK" -ForegroundColor 'Green'
 InstallFilterPack1
 }

else {
 Write-Host "Checksum Error, removing $file1" -ForegroundColor 'Red'
 Remove-Item $file1
 DownloadFilterPack1
 InstallFilterPack1
 }
 }

else {
 #Download Microsoft Filter Pack
 DownloadFilterPack1

#Install FilterPack 1
 InstallFilterPack1
 }

if (Test-Path $file2) {
 Write-Host "The file $file2 exists."

$chksum2 = Get-Checksum $file2
 if ($file2chksum.CompareTo($chksum2) -eq 0) {
 Write-Host "Checksum OK" -ForegroundColor 'Green'
 InstallFilterPack2
 }

else {
 Write-Host "Checksum Error, removing $file2" -ForegroundColor 'Red'
 Remove-Item $file2
 DownloadFilterPack2
 InstallFilterPack2
 }
 }

else {
 #Download Microsoft Filter Pack SP1
 DownloadFilterPack2

#Install Microsoft Filter Pack SP1
 InstallFilterPack2
 }
 }
 }

function InstallUMAPI() {

function InstallMicrosoftUMAPI() {

#Check/Install Media Foundation feature
 $mf = Get-WindowsFeature "Server-Media-Foundation" | select *

Start-Sleep 2

if ($mf.Installed -eq $False) {

Write-Host "Installing Media Foundation feature..."    -ForegroundColor Green
 Add-Windowsfeature Server-Media-Foundation

Write-Host ""
 Write-Host "Installing of Media Foundation feature completed." -ForegroundColor Green
 Write-Host ""
 Write-Host "Restart the server and restart the task" -ForegroundColor Red
 Write-Host "or else the UM API won't be installed" -ForegroundColor Red
 Write-Host ""
 return
 }

if ($mf.Installed -eq $True) {

#Install Microsoft UM API
 Write-Host "Installing Microsoft UM API..." -ForegroundColor Green

$args = "/quiet /norestart"
 $setup = (Start-Process $file -ArgumentList $args -Wait -PassThru).ExitCode
 if ($setup -eq 0) {
 Write-Host "Successfully installed $file" -ForegroundColor Green
 }

if ($setup -ne 0) {
 Write-Host "Failed!" -ForegroundColor Red
 }
 }
 }

function DownloadMicrosoftUMAPI() {

#Download Microsoft UM API
 Write-Host "Downloading Microsoft UM API..." -nonewline
 $clnt = New-Object System.Net.WebClient
 $url = "<a href="http://download.microsoft.com/download/2/C/4/2C47A5C1-A1F3-4843-B9FE-84C0032C61EC/UcmaRuntimeSetup.exe&quot;">http://download.microsoft.com/download/2/C/4/2C47A5C1-A1F3-4843-B9FE-84C0032C61EC/UcmaRuntimeSetup.exe"</a>
 $clnt.DownloadFile($url,$file)
 Write-Host "done!" -ForegroundColor Green
 }

#Checking for regkey
 if (Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\UCMA4" -ErrorAction SilentlyContinue) {

Write-Host "Unified Communications Managed API 4.0 Runtime is already installed." -ForegroundColor yellow
 return
 }

else {

trap {
 Write-Host "Problem downloading UM API. Please visit: <a href="http://www.microsoft.com/en-us/download/details.aspx?id=34992&quot;">http://www.microsoft.com/en-us/download/details.aspx?id=34992"</a>
 break
 }

#set a var for the folder you are looking for
 $folderPath = 'C:\Temp'

#Check if folder exists, if not, create it
 if (Test-Path $folderpath) {
 Write-Host "The folder $folderPath exists."
 }

else {
 Write-Host "The folder $folderPath does not exist, creating..." -NoNewline
 New-Item $folderpath -type directory | Out-Null
 Write-Host "done!" -ForegroundColor Green
 }

# Check if file exists, if not, download it
 $file = $folderPath+"\UcmaRuntimeSetup.exe"
 $filechksum = "5E4D536E916F9C97EE5A91A61952589825AEAA6C"
 if (Test-Path $file) {
 Write-Host "The file $file exists."

$chksum3 = Get-Checksum $file
 if ($filechksum.CompareTo($chksum3) -eq 0) {
 Write-Host "Checksum OK" -ForegroundColor 'Green'

#Install Microsoft UM API
 InstallMicrosoftUMAPI
 }

else {
 #Remove the file with wrong checksum
 Remove-Item $file

#Download Microsoft UM API
 DownloadMicrosoftUMAPI

#Install Microsoft UM API
 InstallMicrosoftUMAPI
 }
 }

else {
 #Download Microsoft UM API
 DownloadMicrosoftUMAPI

#Install Microsoft UM API
 InstallMicrosoftUMAPI
 }
 }
 }

Import-Module ServerManager
 $opt = "None"
 # Do {
 clear
 if ($opt -ne "None") {Write-Host "Last command: "$opt -foregroundcolor Yellow}
 Write-Host
 Write-Host Exchange Server 2013 SP1 - Prerequisites script
 Write-Host Please, select which role you are going to install..
 Write-Host
 Write-Host '1) Client Access Server'
 Write-Host '2) Mailbox'
 Write-Host '3) Typical (CAS/Mailbox)'
 Write-Host
 Write-Host '10) Install Microsoft Filter Pack 2.0'
 Write-Host '    Recommended if installing Mailbox Server roles' -foregroundcolor yellow
 Write-Host '    Automatically set for options 2 and 3' -foregroundcolor yellow
 Write-Host '11) Install Microsoft UM API' -foregroundcolor yellow
 Write-Host '    Automatically set for options 1, 2 and 3' -foregroundcolor yellow
 Write-Host '12) Disable UAC'
 Write-Host '13) Disable Firewall'
 Write-Host
 Write-Host '18) Restart the Server'
 Write-Host '19) End'
 Write-Host
 $opt = Read-Host "Select an option.. [1-19]? "

$Win2008feat = "Desktop-Experience", "NET-Framework", "NET-HTTP-Activation", "RPC-over-HTTP-proxy", "RSAT-Clustering", "RSAT-Web-Server", "WAS-Process-Model", "Web-Asp-Net", "Web-Basic-Auth", "Web-Client-Auth", "Web-Digest-Auth", "Web-Dir-Browsing", "Web-Dyn-Compression", "Web-Http-Errors", "Web-Http-Logging", "Web-Http-Redirect", "Web-Http-Tracing", "Web-ISAPI-Ext", "Web-ISAPI-Filter", "Web-Lgcy-Mgmt-Console", "Web-Metabase", "Web-Mgmt-Console", "Web-Mgmt-Service", "Web-Net-Ext", "Web-Request-Monitor", "Web-Server", "Web-Stat-Compression", "Web-Static-Content", "Web-Windows-Auth", "Web-WMI"
 $Win2012feat = "AS-HTTP-Activation", "Desktop-Experience", "NET-Framework-45-Features", "RPC-over-HTTP-proxy", "RSAT-Clustering", "RSAT-Clustering-CmdInterface", "RSAT-Clustering-Mgmt", "RSAT-Clustering-PowerShell", "Web-Mgmt-Console", "WAS-Process-Model", "Web-Asp-Net45", "Web-Basic-Auth", "Web-Client-Auth", "Web-Digest-Auth", "Web-Dir-Browsing", "Web-Dyn-Compression", "Web-Http-Errors", "Web-Http-Logging", "Web-Http-Redirect", "Web-Http-Tracing", "Web-ISAPI-Ext", "Web-ISAPI-Filter", "Web-Lgcy-Mgmt-Console", "Web-Metabase", "Web-Mgmt-Console", "Web-Mgmt-Service", "Web-Net-Ext45", "Web-Request-Monitor", "Web-Server", "Web-Stat-Compression", "Web-Static-Content", "Web-Windows-Auth", "Web-WMI", "Windows-Identity-Foundation"

switch ($opt) {
 1 {

# Windows Server 2008 R2 SP1
 if ((Get-WMIObject win32_OperatingSystem).Version -eq "6.1.7601") {

InstallUMAPI
 Import-Module ServerManager
 Add-WindowsFeature $Win2008feat -restart
 }

# Windows Server 2012 or Windows Server 2012 R2
 if (((Get-WMIObject win32_OperatingSystem).Version -eq "6.2.9200") -or ((Get-WMIObject win32_OperatingSystem).Version -eq "6.3.9600")) {

InstallUMAPI
 Install-WindowsFeature $Win2012feat -restart
 }
 }

2 {

# Windows Server 2008 R2 SP1
 if ((Get-WMIObject win32_OperatingSystem).Version -eq "6.1.7601") {

Import-Module ServerManager
 InstallUMAPI
 InstallFilterPack
 Add-WindowsFeature $Win2008feat -restart
 }

# Windows Server 2012 or Windows Server 2012 R2
 if (((Get-WMIObject win32_OperatingSystem).Version -eq "6.2.9200") -or ((Get-WMIObject win32_OperatingSystem).Version -eq "6.3.9600")) {

InstallUMAPI
 InstallFilterPack
 Install-WindowsFeature $Win2012feat -restart
 }
 }

3 {

# Windows Server 2008 R2
 if ((Get-WMIObject win32_OperatingSystem).Version -eq "6.1.7601") {

Import-Module ServerManager
 InstallFilterPack
 InstallUMAPI
 Add-WindowsFeature $Win2008feat -restart
 }

# Windows Server 2012 or Windows Server 2012 R2
 if (((Get-WMIObject win32_OperatingSystem).Version -eq "6.2.9200") -or ((Get-WMIObject win32_OperatingSystem).Version -eq "6.3.9600")) {

InstallUMAPI
 InstallFilterPack
 Install-WindowsFeature $Win2012feat -restart
 }
 }

10 {
 # future - auto detect Internet access
 Write-Host 'Can this server access the Internet?'
 $filtpack = Read-Host 'Please type (Y)es or (N)o...'
 switch ($filtpack)                {
 Y { InstallFilterPack }
 N {Write-warning 'Please download and install Microsoft Filter Pack from here: <a href="http://www.microsoft.com/en-us/download/details.aspx?id=26604">http://www.microsoft.com/en-us/download/details.aspx?id=26604</a> and <a href="http://www.microsoft.com/en-us/download/details.aspx?id=17062'}">http://www.microsoft.com/en-us/download/details.aspx?id=17062'}</a>
 }
 }

11 {
 # future - auto detect Internet access
 Write-Host 'Can this server access the Internet?'
 $umapi = Read-Host 'Please type (Y)es or (N)o...'
 switch ($umapi)                {
 Y { InstallUMAPI }
 N {Write-Warning 'Please download and install Microsoft UM API from here: <a href="http://www.microsoft.com/en-us/download/details.aspx?id=34992'}">http://www.microsoft.com/en-us/download/details.aspx?id=34992'}</a>
 }
 }

12 { Disable-UAC }
 13 { Disable-FW }
 18 { Restart-Computer }
 19 {
 Write-Host "Exiting..."
 Exit
 }
 default {Write-Host "You haven't selected any of the available options. "}
 }