# Questions- Comparison, Wildcards,Using If,Else, and For Each

**URL:** <https://forums.powershell.org/t/questions-comparison-wildcards-using-if-else-and-for-each/15202>\
**Category:** PowerShell Help\
**Created:** [November 9, 2020, 7:44am UTC](https://forums.powershell.org/t/questions-comparison-wildcards-using-if-else-and-for-each/15202 "2020-11-09T07:44:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrzac](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@andrzac](https://forums.powershell.org/u/andrzac)\
**Post date:** [November 9, 2020, 7:44am UTC](https://forums.powershell.org/t/questions-comparison-wildcards-using-if-else-and-for-each/15202/1 "2020-11-09T07:44:26Z")

</div>

Would anyone be able to help me with a few questions that I’m stuck on?

1. Comparison: Write the PowerShell code to determine if the numeric value stored in $memory is less than 10. Assume both values are in GB. Hint: you can create a $memory variable to test your comparison.

2. Wildcards: Write the PowerShell code to determine if the string value stored in $servicename starts with ‘bi’. Case does not matter.

3. Using If: Write the PowerShell code to write output to the terminal of ‘Memory is too low!’ if the numeric value stored in $memory is less than 10. Assume both values are in GB. Hint, you can sue the code from #1 above!

4. Using Else: Add to the code in #3 to write output to the terminal of ‘You have enough memory!’ if the condition is not met

. 5. Using ForEach: Assume you have a collection of services in a variable called $services. Using foreach, write PowerShell commands that iterate through the list of services and refreshes each service. Hint: Look for a method on Service objects called ‘Refresh’.

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [November 9, 2020, 8:03am UTC](https://forums.powershell.org/t/questions-comparison-wildcards-using-if-else-and-for-each/15202/2 "2020-11-09T08:03:16Z")

</div>

This looks a lot like a homework assignment, so I’m not going to just give you the answers, but I will point you in the right direction.

1. Comparison: Write the PowerShell code to determine if the numeric value stored in $memory is less than 10. Assume both values are in GB. Hint: you can create a $memory variable to test your comparison.

Get-Help [About\_Comparison\_Operators](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-5.1)

Hint: -lt

1. Wildcards: Write the PowerShell code to determine if the string value stored in $servicename starts with ‘bi’. Case does not matter.

Get-Help [About\_Comparison\_Operators](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-5.1)

Hint: -like

1. Using If: Write the PowerShell code to write output to the terminal of ‘Memory is too low!’ if the numeric value stored in $memory is less than 10. Assume both values are in GB. Hint, you can sue the code from #1 above!

Get-Help [About\_If](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_if?view=powershell-5.1)

Hint: In ISE hold down CTRL and press j. Type if for a code snippet.

1. Using Else: Add to the code in #3 to write output to the terminal of ‘You have enough memory!’ if the condition is not met

Same as above. For writing to the terminal Get-Help Write-Host

. 5. Using ForEach: Assume you have a collection of services in a variable called $services. Using foreach, write PowerShell commands that iterate through the list of services and refreshes each service. Hint: Look for a method on Service objects called ‘Refresh’.

Get-Help [About\_ForEach](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-5.1)

Get-Service | Get-Member (This will show the properties and methods of service objects).

&nbsp;

This should get you well on your way. If you want to post back your answers, I’ll give you some feedback.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [November 9, 2020, 8:05am UTC](https://forums.powershell.org/t/questions-comparison-wildcards-using-if-else-and-for-each/15202/3 "2020-11-09T08:05:04Z")

</div>

We’re not here to do your homework. Post code and what exactly is not working if you want assistance.

---

<div class="post-metadata">

**Author:** ![andrzac](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@andrzac](https://forums.powershell.org/u/andrzac)\
**Post date:** [November 9, 2020, 9:29am UTC](https://forums.powershell.org/t/questions-comparison-wildcards-using-if-else-and-for-each/15202/4 "2020-11-09T09:29:48Z")

</div>

Hi sorry I wasn’t just digging for the answers.

Mike I appreciate you responding to me and for the tips. I’ve figured out the first four, but am stuck on the last one. This is what I have so far.

&nbsp;

for ($services in get-service | select refresh)

{ Write-Host $Services

}

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [November 9, 2020, 9:50am UTC](https://forums.powershell.org/t/questions-comparison-wildcards-using-if-else-and-for-each/15202/5 "2020-11-09T09:50:27Z")

</div>

Since $services is already an array of services per the stem of the question, there is no need to call Get-Service again. Just build your ForEach loop to iterate over $Services. Inside the ForEach loop call the Refersh method of the current object.

```
ForEach ($s in $Services) {
   $s.Refresh()
}
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![andrzac](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@andrzac](https://forums.powershell.org/u/andrzac)\
**Post date:** [November 9, 2020, 10:08am UTC](https://forums.powershell.org/t/questions-comparison-wildcards-using-if-else-and-for-each/15202/6 "2020-11-09T10:08:09Z")

</div>

Ok thanks for your help!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/questions-comparison-wildcards-using-if-else-and-for-each/15202/7 "2024-05-16T20:29:11Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


