Powershell to the people

Arrays and filters— Part 3

Please share this tutorial, that inspires me to continue doing this tutorial series.

Previous parts

Arrays

$basket = “apple”,”orange”,”banana”,”kiwi”

You can print out how many objects are in this example by using the count method which is available in all objects or you can also write out the specified object in specific index of the basket, as seen in the image below.

Image for post
Image for post

As you can see in the image above that we can count how many objects are in the array and also what index of apple is. Now arrays always start from 0 until the last object. You can iterate through the array by using foreach as seen in the example below.

#iterate through the listforeach($fruit in $basket){  write-host "We have $fruit in the basket"}
Image for post
Image for post

This is just a simple example on how we implement an array, when we start to utilize Powershell much more then we can see how arrays are powerfull and how we can use them to help us find data using filters.

Filters

$basket | where { $_ -like "*a*" }
Image for post
Image for post

As you can see in the screenshot above it does not show “orange” or “kiwi” but only the words that contain the letter “a”. Now there are more operators that you can use and the operator in the screenshot above is “-like”. Here are list of common operators that I use regularly

# finds something that is like something
-like
# finds something that is not like something
-notlike
# find something that is exactly this, same as equal
-eq
# find something that is not exactly like this, is not equal.
-ne

There are more operators but I will not get into that now.

Continue with the next part of this series

Part 4

Written by

IT tech geek, developer etc.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store