0

Consider:

$str = 'My {token} string'

$newStr = $str.Replace('{token}', 'value')
$newStr
My value string

function strReplace ($str, $rval) { $str.Replace('{token}', $rval) }
$newStr = strReplace($str, 'value')
$newStr
My  string
value

Even though string.Replace returns a single string, an object[] shows up in the return pipeline. Why? Is there a way to get the obvious return value?

2
  • when I run this code, it get an error with the following line: $newStr = strReplace($str, 'value') but when I replace it with: $newStr = strReplace $str 'value' it works as expected. I'm using Powershell 2.0 with .net 4.0 Commented Feb 21, 2013 at 20:05
  • maybe it is a change with ps3? Commented Feb 21, 2013 at 20:35

1 Answer 1

3

It's because you are passing in an array as the argument for $str in your function. You need to call PowerShell functions like this (no brackets, no commas separating arguments):

$newStr = strReplace $str 'value'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I get the same object[] even after removing the parentheses. I'm using ps3 on Win8.
Did you remember to drop the comma too? spaces are used to seperate arguments

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.