In SQL Server Management Studio, turn on SQLCMD mode from the Query menu.
Then at the top of your script, type in the command below
:Connect server_name[\instance_name] [-l timeout] [-U user_name [-P password]
If you are connecting to multiple servers, be sure to insert GO between connections; otherwise your T-SQL won’t execute on the server you’re thinking it will.