Home > database >  What is SQLCMD in PowerShell?
What is SQLCMD in PowerShell?

Time:03-18

I have been trying to develop a function that handles all the SQLCMD requirements for a complex process. This includes mixed connection modes (trusted or username/password), different databases, statement execution or script file execution, capturing output to variables or to a file for parsing, lots of variables pass-through and generating dynamic SQL to a file (using both select and print statements and very wide output). I had tried and discounted ADO.NET and had been testing between Invoke-SQLCMD and Start-Process -FilePath ...\SQLCmd.exe...

I found a reference to SQLCMD and I copied/pasted it in to powershell, modified it and it worked!

$result = sqlcmd -U uuuuu -P pppppp -i "D:\temp\testscript.sql" -v time="'time'" -S $server -d Master -w 3000

But what is it? When I do get-help SQLCMD, it only lists Invoke-SQLCMD and my functions. When I do get-alias sqlcmd it throws an error as it doesn't exist. I have a $sqlcmd variable but I'm not referencing it like that.

By the way, if anyone knows of a useful SQLCMD function that handles all this, happy to look.

CodePudding user response:

As mentioned in the comments, the command you're looking for is Get-Command:

PS ~> Get-Command sqlcmd |Format-Table -AutoSize

CommandType Name       Version       Source
----------- ----       -------       ------
Application SQLCMD.EXE 15.0.1300.359 C:\Program Files\Microsoft SQL Server\

Under CommandType we can see that indeed it resolves to a native executable application, which is a valid command type in PowerShell (the same ping.exe, cmd.exe or even powershell.exe would be).

  • Related