# Set common variables
$serverTracingRegistry='Registry::HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSCRM'

# Enable CRM Tracing
Add-PSSnapin Microsoft.Crm.PowerShell
$setting = Get-CrmSetting TraceSettings
$setting.Enabled = $True
$setting.Directory = "D:\crmTraceLogs"
Set-CrmSetting $setting
$traceRefreshCurrentValue=(Get-ItemProperty -path $serverTracingRegistry).'TraceRefresh'
Set-Itemproperty -path $serverTracingRegistry -Name 'TraceRefresh' -value 1
Set-Itemproperty -path $serverTracingRegistry -Name 'TraceEnabled' -value 1

# Disable Tracing
Add-PSSnapin Microsoft.Crm.PowerShell
$setting = Get-CrmSetting TraceSettings
$setting.Enabled = $False
Set-CrmSetting $setting
Set-Itemproperty -path $serverTracingRegistry -Name 'TraceRefresh' -value 0
Set-Itemproperty -path $serverTracingRegistry -Name 'TraceEnabled' -value 0
write-host "Must run IISRESET for changes to be effective"

 
# Check current settings
(Get-CrmSetting TraceSettings).Enabled
Get-ItemProperty -path $serverTracingRegistry -Name 'TraceRefresh'
Get-ItemProperty -path $serverTracingRegistry -Name 'TraceEnabled'