How can I avoid including full path for SQL script files when using sqlcmd inside powershell?

  • I have the following lines in my powershell script. The sql-script file is in the same directory as the actual powershell script. Is there a way to avoid having to constantly include the full path to the sql-script file? I find it very annoying...

    I haven't been able to find a satisfactory solution to this on the web.

    $instance = "myServer\myInstance"

    sqlcmd `-S $instance `-i C:\Scripts\POWERSHELL\...\...\checkDB.sql

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]

  • Try adding the location of your scripts to the path environment variable.

    PS C:\>$env:path += ";C:\MyScripts"

  • I've had to do that, or run the script from the directory where I want to store/read all the files. I'm not that aware of any other choices either.

    Adding the path could work in some circumstances.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Thanks guys for your suggestions.

    In the past, I remember running a sql script file on the same folder as the powershell script itself, and getting an error if I did not specify the full path of the sql script.

    I tried it now and it is working without having to include the full path...

    I will post here if I see this happen again.

    In any case, thanks again!

    $instance = "srvr\instName"

    sqlcmd `-S $instance `-i someScript.sql

    write-output 'done'

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply