Create a SQL Server User using Powershell


I’ve written a powershell script that creates a new sql server database and login, and then sets the database owner to the newly created user. This is successful. However, I get a login failed exception when attempting to login within the same script. If I use SQL Server Management Studio the login works.

Here’s the script:

I’ve tried adding a Start-Sleep (up to 5mins) to no avail, and I’ve tried Restart-Service mssqlserver -Force, also to no avail.

Any ideas?


This isn’t an answer to the problem I was encountering, just a work around. The script is being run as part of an automated deployment, the overall scripts are run under the “NT AUTHORITY\SYSTEM” username, so to get around my logging in issue I’m simply using Integrated Security=true.


Create a SQL Server User using Powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply