[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")|Out-Null
[System.Reflection.Assembly]::LoadFrom("C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.SqlWmiManagement.dll")|Out-Null
Write-Host "Assemblies Loaded..."

$wmiserver = New-Object "Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer" 
$SQLService = $wmiserver.Services['MSSQLSERVER'];
$startupParameters = $SQLService.StartupParameters # YMMV on the service name if you use named instances
$startupParam = $startupParameters.split(';') | ? { $_.StartsWith('-P') }
 
if ($startupParam -ne $null) {
	$SQLService.StartupParameters = $startupParameters.Replace(';'+$startupParam, "")
	Write-Host "Startup Parameter Removed..."
	
	$SQLService.Alter()
	$SQLService.Stop();
	Write-Host "Stopping Service..."

	while ($SQLService.ServiceState -ne [Microsoft.SqlServer.Management.Smo.Wmi.ServiceState]::Stopped)
	{
		Start-Sleep -Milliseconds 20;
		$SQLService.Refresh();
		Write-Host "Waiting on Service Stop..."
	}
	Write-Host "Restarting Service..."
	$SQLService.Start();
}
else {
	Write-Host "Parameter does not exist..."
	Start-Sleep -Milliseconds 2500;
}

