2

I am fairly new to PowerShell and I'm struggling to see what's wrong with the following command:

Get-ChildItem -Filter "*Q1 2016.pdf" | For.\pdftk A={$_.name} B={$_.name -replace 'Q1 2016.pdf','Q1 2016-quad.pdf'} cat A1-3 B1 A5-end output {$_.name -replace 'Q1 2016.pdf','Q1 2016-final.pdf'}

I get the following error:

pdftk.exe : The command parameter was already specified.
At line:1 char:40
+ ... 2016.pdf" | .\pdftk A={$_.name} B={$_.name -replace 'Q1 2016.pdf','Q1 ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [], ParameterBindingException
+ FullyQualifiedErrorId : ParameterSpecifiedAlready

However, when I run a single command call, it works fine:

.\pdftk A='myfile-Q1 2016.pdf' B='myfile-Q1 2016-quad.pdf' cat A1-3 B1 A5-end output 'myfile-final.pdf'

Any thoughts of what am I doing wrong?

sodawillow
  • 12,497
  • 4
  • 34
  • 44
user1690166
  • 55
  • 2
  • 6
  • 2
    Possible duplicate of [How to run an EXE file in PowerShell with parameters with spaces and quotes](http://stackoverflow.com/questions/1673967/how-to-run-an-exe-file-in-powershell-with-parameters-with-spaces-and-quotes) – sodawillow May 25 '16 at 17:27

1 Answers1

0

It turns out it should be like this:

Get-ChildItem -Filter “*Q1 2016.pdf” | ForEach {.\pdftk A=$($_.name) B=$($_.name -replace ‘Q1 2016.pdf’,’Q1 2016-quad.pdf’) cat A1-3 B1 A5-end output $($_.name -replace ‘Q1 2016.pdf’,’Q1 2016-final.pdf’)}

Somehow, the following does not work, but the above does:

Get-ChildItem -Filter “*Q1 2016.pdf” | ForEach {.\pdftk A={$_.name} B={$_.name -replace ‘Q1 2016.pdf’,’Q1 2016-quad.pdf’} cat A1-3 B1 A5-end output {$_.name -replace ‘Q1 2016.pdf’,’Q1 2016-final.pdf’}}

user1690166
  • 55
  • 2
  • 6
  • this may be attempting to explain why the $($variable) syntax is needed http://stackoverflow.com/questions/13615676/what-does-variablename-mean-in-powershell – user1690166 May 25 '16 at 17:39