Enable remote desktop on Windows 2012

Quick function to enable remote desktop on a Windows 2012 server even if Remoting is already enabled by default…
I haven’t provided any help in the function as its content is more that explicit (at least for me 😛 )

Function Enable-RemoteDesktop             
{            
[CmdletBinding()]            
param()            
    Begin            
    {            
        # Make sure we run on a Windows 2012 server            
        if (-not($PSVersionTable.BuildVersion -gt ([version]'6.2')))            
        {            
            Write-Warning -Message "This function can only run on Windows 8 or 2012"            
            return            
        }            
        # Make sure we run as admin            
        $usercontext = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()            
        $IsAdmin = $usercontext.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")                               
        if (-not($IsAdmin))            
        {            
            Write-Warning "Must run powerShell as Administrator to perform these actions"            
            return            
        }             
    }             
    Process             
    {            
        # Set the registry value            
        try            
        {            
            Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server' -Name fDenyTSConnections -Value 0 -Force -ErrorAction Stop            
            if ($?)            
            {            
                Write-Verbose -Message "Remote desktop registry value set"            
            }            
        } catch {            
            Write-Warning -Message "Failed to set the registry value that turns on remote desktop"            
        }            
        # Set the firewall rule            
        try            
        {            
            Set-NetFirewallRule -DisplayGroup "Remote Desktop" -Enabled:True -ErrorAction Stop            
            if ($?)            
            {            
                Write-Verbose -Message "Remote desktop firewall rule set"            
            }            
        } catch {            
            Write-Warning -Message "Failed to enable the remote desktop group rules"            
        }            
    }            
}
Advertisements

4 thoughts on “Enable remote desktop on Windows 2012

  1. On the RTM version of WS2012, just replace

    Set-NetFirewallRule -DisplayGroup “Remote Desktop” -Enabled:True -ErrorAction Stop

    by

    Enable-NetFirewallRule -DisplayGroup “Remote Desktop” -ErrorAction Stop

  2. Hi,
    how can I include the command “Set-ItemProperty -Path ‘HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp’ -Name UserAuthentication -Value 0 ”
    ?

    thx for your help

    • Hi,
      You can include it inside the first try/catch block like this:

      # Set the registry value            
              try            
              {            
                  Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server' -Name fDenyTSConnections -Value 0 -Force -ErrorAction Stop            
                  if ($?)            
                  {            
                      Write-Verbose -Message "Remote desktop registry value set"            
                  }            
                  Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name UserAuthentication -Value 0 -Force -ErrorAction Stop
              } catch {            
                  Write-Warning -Message "Failed to set the registry value that turns on remote desktop"            
              }
      

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s