0

I would like to output a line to my .txt file/log file that basically grabs the path of $source then says "is compared too" the path of $compare. But when I do this with Out-File they overwrite each other. For instance, if I was to do $source | Out-File $log with only the path before the recurse the next Out-File over writes it. So by the end of my script when I look at my log all that is there is the Compare-Object | Out-File $logI use below.

   #prompts user for input through cmd for path of log file
    $log = Read-Host -Prompt 'Input path of log, if log does not, create one. Include file name and extension' 

    #prompts user for input through cmd, saves path, then recursively searches
    $source = Read-Host -Prompt 'Input source path'  
    $source = gci $source -recurse

    #prompts user for input through cmd, saves path, then recursively searches
    $compare = Read-Host -Prompt 'Input the path of directory to compare' 
    $compare = gci $compare -recurse

    #compares the $source to the $compare and outputs to log file 

    Compare-Object -ReferenceObject $source -DifferenceObject $compare -PassThru | Out-File $log -width 120

2 Answers 2

1

Use the -Append parameter to add text to a file without overwriting it.

Out-File $log -width 120 -Append
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, not sure how I overlooked that.
No problem, can you accept the answer if it has answered your question.
0

Using the -Append parameter will append your text as a new line.

If you want to have the Text part (x is compared to y) in one Line you will have to assemble the String before outputting it. For example:

"$source is compared to $compare :" | out-file $log -width 120
$comparison | out-file $log -width 120 -Append

Comments

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.