First, we need to keep our credential information so that we do not have to enter it every time we connect to the remote machine.
$cred = Get-Credential
Then, let us check if remote desktop is enabled. This will tell us if we need to enable it.PS C:\temp> Invoke-Command -computername dev-pc -scriptblock {(gwmi -class win32_terminalservicesetting -namespace "root\cimv2\terminalservices").allowtsconnections} -Credential $cred
If we need to enable it, run the following command.PS C:\temp> Invoke-Command -computername dev-pc -scriptblock {(gwmi -class win32_terminalservicesetting -namespace "root\cimv2\terminalservices").setallowtsconnections(1)} -Credential $cred
Then, run the following one to make sure that authentication is required for remote desktop access.PS C:\temp> invoke-command -computername dev-pc -scriptblock {(gwmi -class win32_tsgeneralsetting -namespace "root\cimv2\terminalservices").setuserauthenticationrequired(1)} -Credential $cred
The above step basically enables Remote Desktop on my dev-pc but the trouble I had was that I still could not connect to my dev box because of firewall so we need to take care of that as well.
To do that, we will need to run one more command.PS C:\temp> invoke-command -computername dev-pc -scriptblock {netsh advfirewall firewall set rule group="remote desktop" new enable=Yes } -Credential $cred
Once we execute the last command, we are now able to remote desktop to remote computer.Lastly, let me put two websites that I got the above scripts from to enable remote desktop.