Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing command output in Powershell as string instead of array of strings

Tags:

powershell

I am trying to capture the output of running an external command as a string, but the result returned by Powershell is always an array of strings. It is my understanding that Powershell splits the output on newlines (Windows or Unix line endings) and stores the result in an array, but I would like these newlines to be preserved since this is for an svn pre-commit hook script to detect if a committed file has mixed line endings.

$output = svnlook cat -t $transId -r $repos $filename

At this point line endings have been stripped, and $output is an array of strings, one per line of output.

Redirecting to a file does not seem to work since this normalizes the line endings. How do you tell Powershell to not split a command's output?

Thanks!

like image 397
FunnyItWorkedLastTime Avatar asked Jan 09 '14 18:01

FunnyItWorkedLastTime


1 Answers

Pipe the command output into the Out-String cmdlet:

$output = svnlook cat -t $transId -r $repos $filename | Out-String
like image 51
Ansgar Wiechers Avatar answered Oct 31 '22 09:10

Ansgar Wiechers