Use filter in PowerShell to create a pipe input function _powershell

Source: Internet
Author: User

This article describes using filter when customizing the PowerShell function without using a function. Filter can quickly create a pipe input function, the filter is equivalent to only one process block function.

Filter keywords can be used instead of function keywords to create functions. However, the function created by filter can only be a parameter of the receive pipeline input parameter and is input to the receiving block structure data. So in the function of filter definition, you can't see the input parameters.

In the function defined by the filter, use the $_ variable to traverse all the input objects.

Copy Code code as follows:

Filter Test-applicationprogram
{
if ($_. Mainwindowtitle-ne "")
{
$_
}
}

The results of the implementation are as follows:
Copy Code code as follows:

Ps> get-process | Test-applicationprogram
Handles NPM (k) PM (k) WS (k) VM (M) CPU (s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
787 110040 102008 319 7,91 7600 Chrome
......

The function above is to detect the process that the window caption is not for, and the input parameter must be a variable of the process type. We used the get-process to get all the processes when we were demonstrating, and then when the parameters were passed to filter.

In addition, the function created by the filter is essentially the same as the function it creates. So, the filter function above is the same as the following function.

Copy Code code as follows:

Function Test-applicationprogram
{
Process
{
if ($_. Mainwindowtitle-ne "")
{
$_
}
}
}

Or, inside the PowerShell system, the function that filter creates will be translated into memory in which the functions are stored.

About PowerShell use filter to quickly create a pipe input function, this article on the introduction of so many, I hope to help you, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.