param ($name, $url, $psk, $username, $password, $domain)

if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {     
  $arguments = "& '" + $myinvocation.mycommand.definition + "'"  
  Start-Process powershell -Verb runAs -ArgumentList $arguments  
  Break  
} 

$error.clear()

# Check if connection already exists
$vpn = Get-VpnConnection | where {$_.Name -eq $name}

# Removing old connection if already exists
if ($vpn -ne $null) {
    if ($vpn.ConnectionStatus -ne "Disconnected") {
        Write-Host "Disconnecting from old connection..." -f Red
        rasdial $name /d | Out-Null
    }
	Write-Host "Removing old connection..." -f Red
	Remove-VpnConnection -Name $name -Force
}

# Creating new connection
try {
	Add-VpnConnection -Name $name -ServerAddress $url -TunnelType L2tp -AuthenticationMethod MSChapv2 -EncryptionLevel Optional -L2tpPsk $psk -SplitTunneling -Force
	Write-Host "Connection created successfully" -f Green 
}
Catch {
	$error.clear()
	throw $_
}

# Connect to it
rasdial $name $username $password /domain:$domain