Skip to content

Mention the quasi-interactive, REPL-like behavior of -File - and -Command - and a workaround #11098

Description

Type of issue

Missing information

Feedback

It is reasonable for users - especially those familiar with POSIX-compatible shells such as Bash - to expect -File - to treat its stdin input as a whole as if it were the content of a script file.
-Command - does not have a POSIX-compatible shell counterpart, but the expectation applies analogously.

Unfortunately, both -File - and -Command - exhibit quasi-interactive behavior that seems to be geared toward supporting REPL-like functionality via stdin: see PowerShell/PowerShell#21808 (comment) for a summary.

To get the expected behavior, the following workaround is currently required, which is worth mentioning:

  • Enclose the code in & { ... }

  • Ensure that it is terminated by two trailing newlines.

Example:

  • From PowerShell:
@'
& { 
  Get-Date
  $PROFILE
}

'@ |
  pwsh -NoProfile -Command -
  • From Bash:
pwsh -NoProfile -Command - <<'EOF'
& { 
  Get-Date
  $PROFILE
}

EOF

Note that, without the extra newline (empty line), both commands above currently provide no output, which is a bug discussed in:

Page URL

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Pwsh?view=powershell-7.4

Content source URL

https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/7.4/Microsoft.PowerShell.Core/About/about_Pwsh.md

Author

Sean Wheeler (@sdwheeler)

Document Id

4ccd2aa8-1a49-5b9d-04ee-5d2c61fb3c38

Activity

  1. changed the title [-]Mention the quasi-interactive, REPL-like behavior of `-File -` and `-Command -`[/-] [+]Mention the quasi-interactive, REPL-like behavior of `-File -` and `-Command -` and a workaround[/+] on May 10, 2024
  2. added a commit that references this issue on Jul 23, 2024
    00b0f30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-aboutArea - About_ topics

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions