# Read-host in function add extra new-line

**URL:** <https://forums.powershell.org/t/read-host-in-function-add-extra-new-line/11272>\
**Category:** PowerShell Help\
**Created:** [September 6, 2018, 3:10pm UTC](https://forums.powershell.org/t/read-host-in-function-add-extra-new-line/11272 "2018-09-06T15:10:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vcmirko](https://avatars.discourse-cdn.com/v4/letter/v/a87d85/32.png) [@vcmirko](https://forums.powershell.org/u/vcmirko)\
**Post date:** [September 6, 2018, 3:10pm UTC](https://forums.powershell.org/t/read-host-in-function-add-extra-new-line/11272/1 "2018-09-06T15:10:49Z")

</div>

Who can solve this ?

the code below drives me nuts. It returns the extra “enter” from the input.

function myreadhost($question){

[string]$result

$result = read-host $question

return $result

}

&nbsp;

$x = myreadhost “tellme”

&nbsp;

$x

“$x”

@($x -split “`n”).Count

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [September 6, 2018, 3:16pm UTC](https://forums.powershell.org/t/read-host-in-function-add-extra-new-line/11272/2 "2018-09-06T15:16:53Z")

</div>

return $Result.TrimEnd() should sort it out quickly enough. Can’t say I’ve really run into it as an issue as yet, but then I don’t really use Read-Host very much 🙂

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [September 8, 2018, 1:06pm UTC](https://forums.powershell.org/t/read-host-in-function-add-extra-new-line/11272/3 "2018-09-08T13:06:55Z")

</div>

I do it in below way, If I want to make my parameter as a question with out having mandatory parameter.

```
function myreadhost{
Param(
$Question,
[string]$result = $(read-host $question)
)
return $result
}
```

Making expression as the default value.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [September 8, 2018, 2:24pm UTC](https://forums.powershell.org/t/read-host-in-function-add-extra-new-line/11272/4 "2018-09-08T14:24:32Z")

</div>

You’re returning $result twice in the function. You don’t need the return keyword. So it’s blank the first time, and then it’s set by read-host the second time. Param() is how you declare a variable. Your function can be as simple as:

```
function myreadhost($question) {
  read-host $question
}
```

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [September 8, 2018, 2:39pm UTC](https://forums.powershell.org/t/read-host-in-function-add-extra-new-line/11272/5 "2018-09-08T14:39:59Z")

</div>

Oh, that’s a very good point, js.

Mirko, worth mentioning a little more succinctly, I think – you’re trying to declare a variable at the start of your function. PS doesn’t declare variables like you may be used to from some languages like C# or [VB.NET](http://VB.NET)

At the beginning of your function, as js mentions, the value of the variable is $null, because it hasn’t been set yet. PowerShell will read that as [string]$null, which is an empty string. This causes an empty string to be added to the output, which is producing the extra blank line, _not_ the keypress from Read-Host.

If you simply specify a value or a variable on a line with no assignment statement like that, PS’s default assumption is that you want to **output** the value and happily does that. Unlike C# where you must use a _return_ keyword, any value expression left on a line will **also** be added to the output stream. PS permits functions to return multiple distinct values.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/read-host-in-function-add-extra-new-line/11272/6 "2024-05-16T20:34:23Z")

</div>

Closed on May 16, 2024, 8:34pm UTC


