Alias for "| foreach { "$_" }" in PowerShell
I like to use the following code to emulate the Unix "find" behavior:
开发者_如何学Cls DIRECTORY -recurse -include PATTERN | foreach { "$_" }
In fact, there are a couple of other commands that I'd like to append this | foreach { "$_" }
to. So I'm trying to find a way to make this easier to type. I tried stuff like this:
function xfind {
ls $args | foreach { "$_" }
}
And then I invoked it like so:
xfind DIRECTORY -recurse -include PATTERN
But that seemed to do the wrong thing...
Consider simply to use the -name
switch of the Get-ChildItem
(aka ls
, dir
):
ls DIRECTORY -recurse -include PATTERN -name
This way is native, clean, and effective.
Try this, it can be extended to a full blown advanced function. The key is to pass the parameters to ls by passing all of them using a special variable (PSBoundParameters) which is avaialble in advanced functions:
function xfind {
[cmdletbinding()]
param(
[string[]]$path,
[switch]$recurse,
[string[]]$include
)
ls @PSBoundParameters | foreach { "$_" }
}
精彩评论