4

While using " in my command string, double quotes gets interpreted.

$printername="Intouch Printer"
print($printername)

$command ='D:\spool12\spool.exe '+ $_.FullName + ' "'+ $printername+'"'
print($command)
iex $command

I am getting this while I am executing this code:

> D:\spool12\spool.exe D:\Spool Files-20170113T061110Z\Spool Files\Un
> Readable\creame and  fudge\00143.SPL IntouchPrinter

Rather I want it to be like:

> D:\spool12\spool.exe D:\Spool Files-20170113T061110Z\Spool Files\Un
> Readable\creame and  fudge\00143.SPL "IntouchPrinter"
3
  • 1
    Escape the double quote " with a backtick ` Commented Jan 30, 2017 at 11:30
  • the result is D:\spool12\spool.exe D:\Spool Files-20170113T061110Z\Spool Files\Un Readable\creame and fudge\00144.SPL `Intouch Printer` @vonPryz Commented Jan 30, 2017 at 12:03
  • 1
    @AnujMasand, Check MartinBrandl's answer. That seems like the best choice Commented Jan 30, 2017 at 12:11

3 Answers 3

4

I prefer using a format string in such scenarios:

$command = 'D:\spool12\spool.exe {0} "{1}"' -f $_.FullName, $printername
Sign up to request clarification or add additional context in comments.

Comments

0

Have a read of the About Quoting Rules, it'll cover what you need to know about quotes and escaping them.

In your case you need to use double quotes around the entire command, and then use the backtick to escape the quotes around $printername.

You will also need to use the Subexpression operator $() so that the FullName property evaluates correctly:

$command = "D:\spool12\spool.exe $($_.FullName) `"$printername`""

1 Comment

** I tried this one and worked fine for me. Thanks @James anyway. ** Get-ChildItem -Path C:\WINDOWS\system32\spool\PRINTERS\ -filter *.spl | foreach { $command= "D:\Intouch_Printer_SW\spool12\spool.exe "+ $_.FullName+" `"Intouch Printer`"" iex $command # echo $command del $_.FullName }
0

You could also wrap strings like this:

$command = @"
D:\spool12\spool.exe "Intouch Printer"
"@
iex $command

3 Comments

@ Kirill P. Sir, actually i am trying search all the sub directories for .spl files. Once i get first sub directory i traverse to get .spl file one by one. then i'm trying to convert that spool file into text file using spool.exe with argument as the fullname(absolute path + file name) of that file and in quotes i have to pass a virtual printer name whch will do the job for me. now when i do one by one like this PS D:\spool12> .\spool.exe .\00002.SPL "Intouch Printer" i'm getting the desired result but it is not good as i want to automate my work. **
@AnujMasand that ^^ should have been in the original question. I suggest you update your post with what you actually want by providing a minimal reproducible example
Thanks sir @kirill P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.