Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

not stupid. Imagine if you wrote:

    get-content file.txt
and the result was the file content visible on screen. Then you were happy with your code and you put it in a function for reuse:

    function test {
        get-content file.txt
    }
and now there's no output. PowerShell pipeline is not stdout, get-content doesn't display anything on screen, only the output formatters at the end of the pipeline do. If the function has no output by default then you would see nothing. That would be an annoying behaviour the other way. In PS, braces {} make anonymous codeblocks and they're used often, e.g. in code like

    # square the first 5 numbers
    1..5 | foreach { $_ * $_ }

    # rename some files
    Get-ChildItem | where { $_.Name -notmatch '[0-9]' } | Rename-Item -NewName { $_.Name + "2" }
That would be really annoying if you always had to 'return' results out of scriptblocks. And it would be annoying if scriptblocks in functions behaved differently to scriptblocks in filterscripts or calculated properties.


Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: