Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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"
share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

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