No whitespaces between variables

61 views Asked by At

Newbie to powershell I'm working on a TXT file to exchange data between two processes. I'm using the following rows to extract data from an XML file to the TXT file

$xmlElm.emXML.e_image_storage_entry.efile_reference | Out-File $Workpath\test1.txt -NoNewline
$xmlElm.emXML.e_image_storage_entry.eclient_id | Out-File $Workpath\test1.txt -Append

Now my problem is that there is no blank space between the two variables as I need.

The result is

E:\EM\e_storage\exp6272001752216699873.jpg241505829

... but I need

E:\EM\e_storage\exp6272001752216699873.jpg 241505829

with a space between the two variable. No metter if I must add an echo line or something else... Can anyone help me?

2

There are 2 answers

0
Santiago Squarzon On

Construct the string before outputting to the file, much easier that way:

@(
    $xmlElm.emXML.e_image_storage_entry.efile_reference
    $xmlElm.emXML.e_image_storage_entry.eclient_id
) -join ' ' | Out-File $Workpath\test1.txt -NoNewline

The above joins the values of efile_reference and eclient_id with a space using the -join operator.

Other way to construct the string can be interpolating these values in a expandable string using the $( ) operator:

"$($xmlElm.emXML.e_image_storage_entry.efile_reference) $($xmlElm.emXML.e_image_storage_entry.eclient_id)" |
    Out-File $Workpath\test1.txt -NoNewline
0
sirtao On

This should work: it joins the two values in a single string separated by a single space, then pipes the result to be appended to the chosen file
You can have it on a single line, I split it for clarity.

$xmlElm.emXML.e_image_storage_entry.efile_reference, 
$xmlElm.emXML.e_image_storage_entry.eclient_id  -join ' ' | 
    Out-File $Workpath\test1.txt -Append