I'm learning PowerShell, so I'm doing stuff like this a lot:
Get-Command | Out-String -Stream | ss child
(ss
is an alias for select-string
).
I'm getting tired of typing Out-string -Stream
all the time, so in Unix I'd just create an alias for it. However this doesn't work in Powershell.
- I created it as an alias, and it errored out (I think I understand that).
- I created it as a function, but it swallows all my output
- I created it as a filter, and that also produced no output.
How can I do this?
Using *child* as the argument is probably the best way to do this particular task, but I'm really using this as an example to learn how to write PS functions.
I just want the function to replace the 'Out-string -Stream' part, so that instead of:
Get-Command | Out-String -Stream | ss child
I'll be able to write:
Get-Command | oss | ss child
I'm guessing I need to use ValueFromPipeline, or the $input variable, like Matt shows.
Okay, I got it. I guess I need to explicitly say to read from the pipeline. Instead of:
function oss { Out-String -Stream }
I need to say:
function oss { $input | Out-String -Stream }
I wonder how I missed that in the documentation?
oss
is pre-defined for you.