0

Why is it when I run this command in powershell:

 $output = (svn stat) -match '^\?' # which $output is an [object] in this case.

I get this output in the PowerGUI Script Editor Console when viewing the variable $output:

?       Projects\TestProject\Thisisatest.txt
?       Projects\TestProject\Thisisatest - Copy.txt

but when I run this command:

[string] $output = (svn stat) -match '^\?'

I get these results:

?       Projects\TestProject\Thisisatest.txt ?       Projects\TestProject\Thisisatest - Copy.txt

It seems when using [string] it does some type of encoding where it takes away newlines and I'm not sure why?

UPDATED user978511 here is the the small example code and output

Clear-Host
Set-Location "C:\svn\Development\Projects\TestProjects\build"
$output = (svn stat) -match '^\?'
'$output results'
$output

'$output2 results'
[string] $output2 = (svn stat) -match '^\?'
$output2

Output:

$output results
?       Projects\TestProject\Thisisatest.txt
?       Projects\TestProject\Thisisatest - Copy.txt
$output2 results
?       Projects\TestProject\Thisisatest.txt ?       Projects\TestProject\Thisisatest - Copy.txt

Thanks for anyone that can help.

1
  • And what behavior do you need? Commented Dec 23, 2011 at 15:01

1 Answer 1

4

When PowerShell casts an array to a single string it converts the members to strings and concatenates them with spaces between each.

$output2 = [string]::join([environment]::newline, $output) will do what you want.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that worked! So (svn stat) -match '^\?' is returning an array of strings?
Most command line commands will provide feedback to the user with a sort of WriteLine method. When assigning the output to a variable, Powershell will store each line in an object and make an array out of it. The object array can be down casted to a string array. You can also use the -join operater to simplify the syntax jon provided. Here's an example using the -join operator: $output2 = $output -join [environment]::newline.

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.