Windows Powershell Foreach-object Loops _powershell

Source: Internet
Author: User

Process a pipe object one by one

If you use Get-wmiobject to obtain services in your system, you may also use format-table to format the results for typesetting.

Copy Code code as follows:

PS c:powershell> Get-wmiobject Win32_Service | Format-table Status,displayname
-autosize

Status DisplayName
------ -----------
OK Adobe Acrobat Update Service
OK Application Experience
OK Application Layer Gateway Service
OK Application Host Helper Service
OK Application Identity
OK Application Information
OK Application Management
OK asp.net State Service

But if you want to make more customized processing for each service, use Foreach-object

Copy Code code as follows:

PS c:powershell> Get-wmiobject Win32_Service | Foreach-object {"Name:" + $_. Disp
Layname, "is the ProcessID more than 100:" + ($_. PROCESSID-GT 100)}
Name:adobe Acrobat Update Service, is processid more than 100:true
Name:application experience, are processid more than 100:false
Name:application Layer Gateway Service, are processid more than 100:false
Name:application Host Helper Service, is processid more than 100:true
Name:application Identity, are processid more than 100:true
Name:application information, are processid more than 100:true
Name:application Management, are processid more than 100:false
Name:ASP.NET state Service, are processid more than 100:false

Combining conditional processing

Foreach-object processing can contain arbitrary PowerShell scripts, including conditional statements, of course

Copy Code code as follows:

Get-wmiobject Win32_Service | Foreach-object {
if ($_. PROCESSID-GT 3000)
{' {0} ({1}) '-F $_. Displayname,$_. ProcessID}
}
Windows presentation Foundation Font Cache 3.0.0.0 (5408)
Microsoft Network Inspection (5260)
BranchCache (4112)
Windows Modules Installer (7656)

Call method

In Foreach-object, $_ represents the current object and, of course, allows the method supported by the object to be invoked through $_.
The following example kills all IE browser processes:

Copy Code code as follows:

PS c:powershell> get-process IExplore

Handles NPM (k) PM (k) WS (k) VM (M) CPU (s) Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
883 14728 22432 181 34.26 4300 IExplore
771 55552 129152 425 8.56 5732
1216 Wuyi 104324 143916 539 572.41 5912 iexplore
801 49200 25372 285 5.99 6252
691 57564 95796 333 8.08 6388
1256 85848 127012 379 20.37 7856 IExplore

PS c:powershell> get-process IExplore | Foreach-object {$_.kill ()}
PS c:powershell> get-process IExplore
Get-process: The process named "IEXPLORE" was not found. Please verify the process name and then call the cmdlet again.
Location: 1 Characters: 12
+ get-process <<<< iexplore
+ Categoryinfo:objectnotfound: (iexplore:string) [get-process],
Processcommandexception
+ Fullyqualifiederrorid:noprocessfoundforgivenname,microsoft.powershell.
Commands.getprocesscommand

Related Article

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.