# Code explanation

**URL:** <https://forums.powershell.org/t/code-explanation/23653>\
**Category:** PowerShell Help\
**Created:** [February 27, 2024, 6:06am UTC](https://forums.powershell.org/t/code-explanation/23653 "2024-02-27T06:06:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![b-affoe](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/b-affoe/32/5214_2.png) [@b-affoe](https://forums.powershell.org/u/b-affoe)\
**Post date:** [February 27, 2024, 6:06am UTC](https://forums.powershell.org/t/code-explanation/23653/1 "2024-02-27T06:06:58Z")

</div>

```auto
function Read-Character() {
    if ($host.ui.RawUI.KeyAvailable) {
    return $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").Character
}
    return $null
}

while($true){
    $char = Read-Character 
    if ($char -ne $null){
        write-host "You pressed $char"
    }
}

```

When I run this code without the while loop, user input cannot be captured, but with the while loop a user input can be capture. Kindly help me understand this

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 27, 2024, 7:59am UTC](https://forums.powershell.org/t/code-explanation/23653/2 "2024-02-27T07:59:52Z")

</div>

B Affoe,  
Welcome to the forum. 👋🏼

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( \</\> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

> [@b-affoe](#):
>
> user input cannot be captured

What do you mean exactly by that? It works for me just as expected. If I run the code without the loop the code captures one key hit and outputs it. 🤷🏼‍♂️

What would you expect? 🤔

---

<div class="post-metadata">

**Author:** ![b-affoe](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/b-affoe/32/5214_2.png) [@b-affoe](https://forums.powershell.org/u/b-affoe)\
**Post date:** [February 28, 2024, 8:36am UTC](https://forums.powershell.org/t/code-explanation/23653/3 "2024-02-28T08:36:35Z")

</div>

```auto
function Read-Character() {
    if ($host.ui.RawUI.KeyAvailable) {
    return $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").Character
}
    return $null
}

$char = Read-Character 
write-host "You pressed $char"

```

When I say user input cannot be captured outside the loop, the script return $null immediately after running the script without giving opportunity for me to press any key. Try running the script above

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 28, 2024, 9:07am UTC](https://forums.powershell.org/t/code-explanation/23653/4 "2024-02-28T09:07:27Z")

</div>

> [@b-affoe](#):
>
> the script return $null immediately after running the script without giving opportunity for me to press any key.

Because you’re not fast enough. 😉 😜 … the code does not wait for a key press. It just checks if there is one. And if there is none the moment it runs it returns `$null` 🤷🏼‍♂️

---

<div class="post-metadata">

**Author:** ![b-affoe](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/b-affoe/32/5214_2.png) [@b-affoe](https://forums.powershell.org/u/b-affoe)\
**Post date:** [February 28, 2024, 9:11am UTC](https://forums.powershell.org/t/code-explanation/23653/5 "2024-02-28T09:11:11Z")

</div>

Sure I might not be fast enough but why is it that when I’m inside a loop I have almost all the time to input a character. That is why I need the explanation

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [February 28, 2024, 9:25am UTC](https://forums.powershell.org/t/code-explanation/23653/6 "2024-02-28T09:25:37Z")

</div>

BTW: I actually did not test your script without the loop before. I did it now and it works just as expected. It waits for a key stroke and outputs it. 🤷🏼‍♂️

---

<div class="post-metadata">

**Author:** ![b-affoe](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/b-affoe/32/5214_2.png) [@b-affoe](https://forums.powershell.org/u/b-affoe)\
**Post date:** [February 28, 2024, 9:35am UTC](https://forums.powershell.org/t/code-explanation/23653/7 "2024-02-28T09:35:26Z")

</div>

Alright, thanks for the feedback

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 29, 2024, 9:35am UTC](https://forums.powershell.org/t/code-explanation/23653/8 "2024-03-29T09:35:59Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
