Question:
I want to use a powershell variable for a cmd argument but I don’t know how to make it.
1 2 3 4 5 6 7 |
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
Answer:
Single quotes (‘ ‘) do not expand variable values in the string. You can address this by either using double quotes (” “):
1 2 |
$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:
1 2 |
$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:
1 2 |
$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit="{0}" "{1}"' -f $newcredit, $file.FullName |