dbatools is a free PowerShell module with over 100 SQL Server administration, best practice and migration commands included.


Copy-SqlLinkedServer

By using password decryption techniques provided by Antti Rantasaari (NetSPI, 2014), this script migrates SQL Server Linked Servers from one server to another, while maintaining remote login names and passwords.

This command requires sysadmin access on both the SQL Server and the Windows server. If accessing remote servers, Remote Registry must enabled and accessible by the account running the script.

Credit: Antti Rantasaari, NetSPI
License: BSD 3-Clause

By default, all linked servers are migrated. The -LinkedServers parameter is autopopulated for command-line completion and can be used to copy only specific linked servers.

If the linked server already exists on the destination, it will be skipped unless -Force is specified. Force drops and recreates the Linked Server.

Screenshots

copy-sqllinkedserver2

Examples

To copy all LinkedServers from sqlserver2014a to sqlcluster, using integrated (Windows) credentials. If Linked Servers with the same name exist on sqlcluster, they will be skipped.

To copy only two LinkedServers, mySQL1 and lssvr1, from sqlserver2014a to sqlcluster, using SQL credentials for sqlserver2014a and Windows authentication for sqlcluster. If a Linked Server with the same name exists on sqlcluster, it will be dropped and recreated because -Force was used.

To see what would happen if the command were executed. No actions are actually performed.

From PowerShell, execute   Get-Help Copy-SqlLinkedServer -Detailed   for more information on this function. Want to see the source code? See it on GitHub