5

I want to use a powershell variable for a cmd argument but I don't know how to make it.

function iptc($file)
{        
        $newcredit = correspondance($credit)
        $cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit=$newcredit $file.FullName'
        Invoke-Expression $cmd
}

For example newcredit can be "James" but in my case when I run the command -Credit will only be "$newcredit".

Regards

1
  • 1
    Single quotes (' ') do not expand variable valuse. Use double quotes (" ") instead or use string formatting (-format). Commented May 28, 2013 at 14:45

1 Answer 1

7

Single quotes (' ') do not expand variable values in the string. You can address this by either using double quotes (" "):

$cmd = "& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit=$newcredit $file.FullName"

Or, by the method I most often use, by using string formatting:

$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit={0} {1}' -f $newcredit, $file.FullName

If either of the parameters has a space in it then the parameter will need to be surrounded by double quotes in the output. In that case I would definitely use string formatting:

$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit="{0}" "{1}"' -f $newcredit, $file.FullName
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. I have another problem, when $newcredit is composed of 2 words (like "James Bond"), -Credit only takes "James", what can I do for that please ?
Thanks it works but I have to put a backtick in double quotes for new credit : -Credit="{0}" otherwise it doesn't work and now if I have two spaces it doesn't work or if there isn't any space it will be like "James"`, weird. As I have understood it's way too hard : link
If you are having to escape the double quote inside the string, then you must have used double quotes on the outside of the string. If double quotes in the parameter cause exiftool to choke then you cannot give that program a parameter with spaces. Did you run exactly the line that I posted?
Yes I run exactly the line you posted but I have an error : + CategoryInfo : ParserError: (:String) [Invoke-Expression], IncompleteParseException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString,Microsoft.PowerShell.Commands.InvokeExpressionCommand
Add a line to your script to Write-Host $cmd out to the console. Copy and paste what it spits out. Will that command execute?
|

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.