Let's start with normal behavior. When I run Read-Host without a hint, I can enter a line starting with an exclamation point:
PS C:\> Read-Host | Foreach-Object { Write-Host 'Entered' $_ } !hi, mom Entered !hi, mom
(Note that I'm just connecting to the Foreach-Object as an easy way to prefix the output. The behavior of interest is the same if you run the Read-Host without it.)
But if I give the Read-Host prompt argument, the behavior is completely different:
PS C:\> Read-Host 'Enter something' | Foreach-Object { Write-Host 'Entered' $_ } Enter something: !hi, mom "!hi, mom" cannot be recognized as a valid Prompt command. Enter something: !!hi, mom Entered !hi, mom
Exclamation seems to allow me to do something other than a simple string type. PowerShell interprets the exclamation, meaning that I enter some kind of command to run it, but I can not find the documentation about what is allowed. Besides doubling the exclamation to avoid it, I cannot figure out what is a valid command.
Please note that input must begin with an exclamation point. Terminating with it does not cause this behavior:
PS C:\> Read-Host 'Enter something' | Foreach-Object { Write-Host 'Entered' $_ } Enter something: hi, mom! Entered hi, mom!
So what can I do with ! here? What is a valid team, except that it simply eludes exclamation? The work around is helpful, but I'm really curious if I can execute code or something like that.
I use PowerShell 4, but that seems a date back a lot earlier.