"Explore PowerShell" "Ten" circular statements

Source: Internet
Author: User
Tags foreach

PowerShell, as a programmable language, has the following loop statement.

Note: This section of the content to discuss the substance more biased in the design aspect, so do not do too much detail in this, only for the application of PowerShell specific explanation.

for (initial value; expression; assignment statement) {code} controlling execution times with variable values

foreach (member variable in array) {code} execute code with iteration

Foreach-object performs an operation on each object of a set of inputs

while (expression) {code} expression is true loop code execution

do {code} while (expression) is similar to while, only executes the code first, then judges the expression true and false

do {code} until (expression) executes code until the expression is false

The application of circular statement in PowerShell

Use foreach to query hardware information

Example one:

$DiskDrive=get-wmiobject -class Win32_DiskDrive -namespace root\CIMV2

foreach ($item in $DiskDrive)
{
write-host "Description:" $item.Description
write-host "Device ID:" $item.DeviceID  
write-host "Interface Type:" $item.InterfaceType
write-host "Media Type:" $item.MediaType
write-host "Model:" $item.Model
write-host "Partitions:" $item.Partitions
write-host "Size:" $item.Size
write-host "Status:" $item.Status
}

Run Result:

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.