How to copy database from one server to another with script
Use dbatools powershell module. There are numerous commands to help all sorts of administrations tasks.

Here is a migrate database example.

copy-dbadatabase -Source oldserver -Destination newserver -BackupRestore -SharedPath \\server\backup\ -Database databasename -SetSourceOffline