Powershell variable for cmd argument

11,257

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
Share:
11,257
Pred
Author by

Pred

Updated on June 04, 2022

Comments

  • Pred
    Pred almost 2 years

    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

  • Pred
    Pred almost 11 years
    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 ?
  • Pred
    Pred almost 11 years
    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
  • EBGreen
    EBGreen almost 11 years
    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?
  • Pred
    Pred almost 11 years
    Yes I run exactly the line you posted but I have an error : + CategoryInfo : ParserError: (:String) [Invoke-Expression], IncompleteParseException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString,Microsoft.PowerShell.Command‌​s.InvokeExpressionCo‌​mmand
  • EBGreen
    EBGreen almost 11 years
    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?
  • Pred
    Pred almost 11 years
    When I write-host $cmd, I get : & C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit="James Bond" "C:\Archive2\cat.jpg" but it won't execute, says doesn't have a terminator, weird.
  • EBGreen
    EBGreen almost 11 years
    Work out how to run the command and get it to work in the console then you can work out how to script it.
  • EBGreen
    EBGreen almost 11 years
    You should also be aware that there are lots of scripts on the web showing how to read and edit exif data using powershell.