Question

IN POWERSHELL     write a script that retrieves a collection of objects (an array) loops them through...

IN POWERSHELL     write a script that retrieves a collection of objects (an array) loops them through a foreach loop and displays one property of the object with the write-host cmdlet.

Homework Answers

Answer #1

Following is a script in powershell that initiates a array of fruits and loops through them by a foreach loop and displays the object using write-host.

here,

  • $fruits = @("mango","apple","banana","guava","peaches","melons")
    this statement declares the list of string objects, we have declared 6 objects
  • foreach ($fruit in $fruits)
    this statement will use a for each loop on the above fruits object, here fruit variable is set as name of object being traversed.
  • Write-Host $fruit
    here, this statement will print the fruit object being traversed using write-host

TO RUN : & ".\run.ps1"

FILE : run.ps1

$fruits = @("mango","apple","banana","guava","peaches","melons")

 foreach ($fruit in $fruits) {
   Write-Host $fruit
 }

OUTPUT:

mango
apple
banana
guava
peaches
melons

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields and appropriate types and permissions diceType numSides sideUp The class should have A 0 argument (default) constructor default values are diceType: d6, numSides: 6, sideUp: randomValue 1 argument constructor for the number of sides default values are diceType: d{numSides}, sideUp: randomValue 2 argument constructor for the number of sides and the diceType appropriate accessors and mutators *theoretical question: can you change the number of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT