In SQL Management Studio there is an option to start PowerShell via right-click on a database (for example).

However, I cannot run queries from that PowerShell session because, it claims, the login fails for my (domain) account.

I am logged in with the same account in Management Studio and started PowerShell from the Management Studio. How can I use the PowerShell session to do anything in SQL Server?

Command I tried was

invoke-sqlcmd "use sometable"

PowerShell then announced that it is using the "provider context" and deduces the SQL server name correctly. But then the query fails because "login failed for user 'mydomain\myaccount'".

What am I doing wrong?

  • Are you logged in to that specific instance of sqlserver with Windows authentication? – mattypenny Oct 25 '12 at 11:06
  • Yes. I started PowerShell from within SQL Management Studio by right-clicking on a database I could see/select from/update. In fact the error message specifically mentions my account which I am using. – Andrew J. Brehm Oct 25 '12 at 14:59

Do you have a database named "sometable"? The USE statement defines database context.

Try this:

Invoke-SqlCmd "select top 1 name from sys.tables"
  • The problem is the "login failed" message, not the contents of the command. I have a database called "sometable". But that's not the point. If I tried your example, I still wouldn't be logged in. Queries only work when the user is logged in. – Andrew J. Brehm Oct 24 '12 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.