We help IT Professionals succeed at work.

SQL Powershell Querie that can be executed by windows schueduler

John-S Pretorius
John-S Pretorius asked
I am trying to create a PowerShell SQL query for example below :

USE users_db
Update users set name = 2 where category = '5'

I then somehow would like be able to use Microsoft scheduler to execute this command / '.bat file' ?
Watch Question

So you're using SQL Server 2008.

* With that comes a tool called SSIS.
* The interface for designing jobs like the one you want to create and then run them using SSIS is called BIDS, and you should be able to find it in your menu structure, somewhere under the SQL Server entries.
* You can use BIDS to create a new SSIS package and include in that an "Execute SQL" job.
* For the Execute SQL task you'll need to create the definition of a connection to the database you want to use: there's a wizard that will help you do that.
* Then, in the interface wizard for the Execute SQL task, you can type in your SQL:

Update users set name = 2 where category = '5'

(You'll already be using User_db, etc., and there'll be no need for the "Go".

* Finally you can run your new package in BIDS to test it.

* If all that (which isn't really very much, actually - it takes more to write about it than to do it!), you'll be able to create a SQL Server Agent Job that will execute your package and schedule that in the SQL Server Agent. Look here for a step-by-step instruction for deployment.here - they're much better at that than I am!

hope this helps

John-S PretoriusSystems Engineer


I am using SQL 2008, which is locked down to limited functionality in a parking environment.

Thank you for your input Mike, I am fully aware of using this approach but with the closed system I'm using in a parking environment that feature is not available (locked-down) indefinably. I do however have available Powershell and scheduler which is what I have been looking into but need a little assistance putting together a complete .bat script that will make the connection, process the sql query and close up again.

Also, I would be scheduling this on the same PC running SQL.
Vitor MontalvãoIT Engineer
Distinguished Expert 2017

Just save the code as a SQL file and the use the SQLCMD utility to run it inside a schedule task.
Example of the SQLCMD utility (run form command prompt) to run a script in a SQL Server instance using trusted connection (-E):
sqlcmd -S ServerName\InstanceName -E -i C:\Path\SQLScript.sql

Open in new window

Marten RuneSQL Expert/Infrastructure Architect

Save the file somwhere as <Filenamewhatever>.PS1
The in Windows scheduler, create a new basic task
copy and past this code into the execute part of windows scheduled task (don't have a English computer nearby, so do an educated guess), the code comes here:

Powershell -ExecutionPolicy bypass -file <FullPath>\<Filenamewhatever>.PS1

This will launch it, and bypass executionpolicy restricted bye the way. So its safer than fiddeling with Set-Executionpolicy unrestricted.

To make it a lot safer, let windows scheduled task run as a domain user (with low level rights) and the powershell file should be encrypted with this users EFS key. This means nobody else can alter the code that windows scheduled task executes. This would be considered a secure solution in most Companys.

Regards Marten
John-S PretoriusSystems Engineer


Thank you for your input, I need direction/assistance creating this .PS1 file please.
SQL Expert/Infrastructure Architect
Save the stuff between the *** in a PS1 file, and execute it
(remember to fill in $SQLServer = '<EnterSQLServerNameHere>'
and check that the DB is named users_db)

$SQLServer = '<EnterSQLServerNameHere>'
$SQLDB = 'users_db'

function QueryDB($DBServer, $Database, $Query, $BatchName) {
#write-host $DBServer, $BatchName
      $connection = new-object system.data.sqlclient.sqlconnection($connectionString)
      $connection.connectionString = $("Data Source=" + $DBServer + ";Initial Catalog=" + $Database + " ;Trusted_Connection=True; Connect Timeout=0; Application Name=" + $BatchName + ";" )
      $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
      $SqlCmd.CommandText = $query
      $SqlCmd.Connection = $connection
      $SqlCmd.CommandTimeout = 0
      $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
      $SqlAdapter.SelectCommand = $SqlCmd
      $DataSet = New-Object System.Data.DataSet
      $script:Supress = $SqlAdapter.Fill($DataSet)
      return ,$DataSet.Tables[0]

QueryDB $SQLServer $SQLDB  "Update users set name = 2 where category = '5'" "ApplicationName_GoodAsProfilerFilter"

Regards Marten
John-S PretoriusSystems Engineer


Thank you much.