2

I'm writing a PowerShell file that creates a batch file having variables between texts that includes quotation marks.

Please see below the script so that you can understand what I mean.

$addnewcomputerhere = Read-Host -Prompt 'Input new computername'
$addnewusernamehere = Read-Host -Prompt 'Input new username'
Write-Output 'powershell -Command "(gc sourcechangeme.txt) -replace ''newcomputername'', '''$addnewcomputerhere'''| Out-File source1changeme.txt"' | Add-Content edittextdone.bat
Write-Output 'powershell -Command "(gc sourcechangeme.txt) -replace ''newusername'', '''$addnewusernamehere'''| Out-File source1changeme.txt"' | Add-Content edittextdone.bat

Running the script edittextdone.bat should then look like this:

powershell -Command "(gc sourcechangeme.txt) -replace 'newcomputername', 'thecomputernamethathasbeengivenonprompt'| Out-File source1changeme.txt"
powershell -Command "(gc sourcechangeme.txt) -replace 'newusername', 'theusernamethathasbeengivenonprompt'| Out-File source1changeme.txt"

Instead of that it looks like this:

powershell -Command "(gc sourcechangeme.txt) -replace 'newcomputername', '
thecomputernamethathasbeengivenonprompt'| Out-File source1changeme.txt"
powershell -Command "(gc sourcechangeme.txt) -replace 'newusername', '
theusernamethathasbeengivenonprompt'| Out-File source1changeme.txt"

My problem is that the variables start new lines.

I was searching for any advice and went through this with no luck.

I've also read about -NoNewline, but couldn't figure out how to use it correctly.

Community
  • 1
  • 1
  • So you are running Powershell to create a batch file that runs Powershell again? Not quite understanding the point of that. This question really no batch-file code, so I would remove the batch-file tag. – Squashman Oct 03 '16 at 12:23

1 Answers1

1

Don't rely on implicit string concatenation. Use the format operator (-f) for inserting values into your output strings:

'powershell -Command "(gc sourcechangeme.txt) -replace ''newcomputername'', ''{0}''| Out-File source1changeme.txt"' -f $addnewcomputerhere | Add-Content edittextdone.bat
'powershell -Command "(gc sourcechangeme.txt) -replace ''newusername'', ''{0}''| Out-File source1changeme.txt"' -f $addnewusernamehere | Add-Content edittextdone.bat
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • It's a good idea, however this didn't work either. I've got the following result: powershell -Command "(gc sourcechangeme.txt) -replace 'newcomputername', '{0}'| Out-File source1changeme.txt" -f userinputdata powershell -Command "(gc sourcechangeme.txt) -replace 'newusername', '{0}'| Out-File source1changeme.txt" -f userinputdata I've tried to move the -f operator right behind the variable but it's still the same. it brakes both lines to 2 more with -f in the second line and the userinputdata on the 3rd line – Andor Armos Oct 03 '16 at 13:52
  • @AndorArmos You didn't remove the `Write-Output` from your generator script. If you want to keep using `Write-Output` you must put parentheses around the formatting expression: `Write-Output ('...' -f $var)` – Ansgar Wiechers Oct 03 '16 at 15:03