Powershell variable for cmd argument

argumentscmdparameterspowershell

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

Best Solution

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