0

I want to pass file as argument to:

Invoke-Sqlcmd $SqlQuery -InputFile $sqlPath

The file is located in different folder. Parent Folder -> Different Folder -> Desired File

I accessed the Parent Folder using:

$sqlPath = Split-Path -Parent $PSScriptRoot

How can I add the final part to $sqlPath? It's my frist script and I have no idea how to add \DifferentFolder\DesiredFile.sql (string?) to this variable.

1
  • 1
    The inverse of Split-Path is Join-Path. Commented May 20, 2019 at 10:21

2 Answers 2

1

You can use string interpolation to do this:

$sqlPath = "$(Split-Path $PSScriptRoot -Parent)\DifferentFolder\DesiredFile.sql"

If you place variables in a double-quoted string, PowerShell will replace them for you at run-time. Even more flexible is the ability to place executable code inside a string (as shown in my example), using a sub-expression $(...). However, be careful not to make the string unreadable with too much code. In some cases it is better to split this into multiple steps:

$parentFolder = Split-Path $PSScriptRoot -Parent
$sqlPath = "$parentFolder\DifferentFolder\DesiredFile.sql"
Sign up to request clarification or add additional context in comments.

Comments

1

What your after is how to do 'string concatenation', basically adding two strings to form a longer one, PowerShell has a few methods, but + works.

You also said you want to update $sqlpath, so

   $sqlpath = $sqlpath + "\DifferentFolder\DesiredFile.sql"

Should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.