I've got this currently on a single line in Powershell ISE

Start-Process -filepath "C:\Powershell-Backup\disk2vhd.exe" -argumentlist 'C: \\Computer1\ServerBackups$\' + $ComputerName + '\' $JobTitle + '.vhd  /accepteula'

and at the moment, this line won't run. The variables are assigned to something, so no problem there.

What I want to happen is for the process to start but with the variables filled in, for example

Start-Process -filepath "C:\Powershell-Backup\disk2vhd.exe" -argumentlist 'C: \\Computer1\ServerBackups$\MyComputer\Backup Operation 12/14/2014 1:55 PM.vhd  /accepteula'

The error I receive in the log is

Start-Process : A positional parameter cannot be found that accepts argument '+'.
At C:\Powershell-Backup\script.ps1:10 char:14
+ Start-Process <<<<  -filepath "C:\Powershell-Backup\disk2vhd.exe" -argumentlist 'C: \\Computer1\ServerBackups$\' + $ComputerName '\' $JobTitle '.vhd  /accepteula'
+ CategoryInfo          : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

How can this be achieved?

  • umm, C: \\ is typo? – hjpotter92 Dec 14 '14 at 14:07
  • No, when I did some research it said it had to be like that. C: is the drive to copy, and \\Computer1 is what I want to copy to (over network...) Even upon changing the error still occurs – EdG Dec 14 '14 at 14:08

First, this is an excellent place to use a string formatter instead of losing your sanity over escaping and interpolation. Second, instead of Start-Process you should be able to use the & statement:

$vhdPath = '\\Computer1\ServerBackups$\{0}\{1}.vhd' -f $ComputerName, $JobTitle
& C:\Powershell-Backup\disk2vhd.exe C: $vhdPath

or If it must be a oneliner

& C:\Powershell-Backup\disk2vhd.exe C: ('\\Computer1\ServerBackups$\{0}\{1}.vhd' -f $ComputerName, $JobTitle)
  • The one-liner worked best for me. Thanks! – EdG Dec 29 '14 at 20:19

A method that tends to be more of a best practice in my opinion that also offers easier ways of troubleshooting issues is to build the string in a variable and then pass that variable to your final command. In this way you can verify the string is being built properly before passing it to the command. Trying to get PowerShell to build a dynamic path or string within the parameter of a cmdlet does not always work out like you think it will everytime.

So I would do something like this:

$argList = "C: \\Computer1\ServerBackups$\$ComputerName\$JobTitle.vhd /accepteula"
Start-Process -filepath "C:\Powershell-Backup\disk2vhd.exe" -argumentlist $argList

One thing to point out is that PowerShell treats strings with variables included differently based on whether it is wrapped in single-quotes or double-quotes. If you place a variable in a string with single-quotes PowerShell treats it as-is, it is not going to resolve that variable to what is assigned before building the string. Whereas, if you use double-quotes PowerShell will parse the string for those variables and resolve them to what is assigned before finalizing the string. You can see a good example of this on WindowsITPro Magazine article: Single Quotes vs. Double Quotes

  • You need to escape the $ inside double quotes for ServerBackups`$. – Matt Dec 15 '14 at 16:35
  • @matt no I don't. Worked fine on my machine – Shawn Melton Dec 15 '14 at 16:50
  • Guess its not a conflict since its followed by a backslash – Matt Dec 15 '14 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.