Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell variable for cmd argument

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

like image 773
Pred Avatar asked May 28 '13 14:05

Pred


People also ask

How do I pass a command line argument in PowerShell?

ps1 and execute this script we need to open PowerShell and type the file name with the parameters. If you are using the command line then to execute the PowerShell script you could use the below format. powershell.exe -File "C:\example. ps1" arg1 arg2 arg3 # OR powershell.exe -File "C:\example.

What is the use of $_ in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

What does %% mean in PowerShell?

% is an alias for the ForEach-Object cmdlet. An alias is just another name by which you can reference a cmdlet or function.

How do you pass a variable in PowerShell?

You can pass variables to functions by reference or by value. When you pass a variable by value, you are passing a copy of the data. In the following example, the function changes the value of the variable passed to it. In PowerShell, integers are value types so they are passed by value.


1 Answers

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
like image 193
EBGreen Avatar answered Sep 22 '22 20:09

EBGreen