fbpx Windows | HostingUltraso.com
24/7/365 Support

Windows

Find Items in an Array Greater or Less Than a Value

Problem

You have an array and want to find all elements greater or less than a given item or value.

Solution

To find all elements greater or less than a given value, use the –gt, ge, lt, and –le comparison operators:

PS >$array = "Item 1","Item 2","Item 3","Item 1","Item 12" PS >$array ge "Item 3" Item 3 PS >$array lt "Item 3" Item 1 Item 2 Item 1 Item 12

Discussion

Remove Elements from an Array

Problem

You want to remove all elements from an array that match a given item or term— either exactly, by pattern, or by regular expression.

Solution

To remove all elements from an array that match a pattern, use the –ne, notlike, and –notmatch comparison operators as shown in Example 112.

Example 112. Removing elements from an array using the –ne, notlike, and –notmatch operators

Find Items in an Array That Match a Value

Problem

You have an array and want to find all elements that match a given item or term— either exactly, by pattern, or by regular expression.

Solution

To find all elements that match an item, use the –eq, like, and –match comparison operators:

PS >$array = "Item 1","Item 2","Item 3","Item 1","Item 12" PS >$array eq "Item 1" Item 1 Item 1 PS >$array like "*1*" Item 1 Item 1 Item 12 PS >$array match "Item .." Item 12

Discussion

Combine Two Arrays

Problem

You have two arrays and want to combine them into one.

Solution

To combine PowerShell arrays, use the addition operator (+):

PS >$firstArray = "Element 1","Element 2","Element 3","Element 4" PS >$secondArray = 1,2,3,4 PS > PS >$result = $firstArray + $secondArray PS >$result Element 1 Element 2 Element 3 Element 4 1 2 3 4

Discussion

One common reason to combine two arrays is when you want to add data to the end of one of the arrays. For example:

PS >$array = 1,2 PS >$array = $array + 3,4 PS >$array 1 2 3 4

Determine Whether an Array Contains an Item

Problem

You want to determine whether an array or list contains a specific item.

Solution

To determine whether a list contains a specific item, use the –contains operator:

PS >"Hello","World" contains "Hello" True PS >"Hello","World" contains "There" False

Discussion

The –contains operator is a useful way to quickly determine whether a list contains a specific element. To search a list for items that instead match a pattern, use the –match or –like operators.

Sort an Array or List of Items

Problem

You want to sort the elements of an array or list.

Solution

To sort a list of items, use the SortObject cmdlet: PS >GetChildItem | SortObject Descending Length | Select Name,Length

Name
Length

ConvertTextObject.ps1
6868

ConnectWebService.ps1
4178

SelectFilteredObject.ps1
3252

GetPageUrls.ps1
2878

GetCharacteristics.ps1
2515

GetAnswer.ps1
1890

NewGenericObject.ps1
1490

InvokeCmdScript.ps1
1313

Discussion

Visit Each Element of an Array

Problem

You want to work with each element of an array.

Solution

To access each item in an array onebyone, use the ForeachObject cmdlet:

PS >$myArray = 1,2,3 PS >$sum = 0 PS >$myArray | ForeachObject { $sum += $_ } PS >$sum 6

To access each item in an array in a more scriptlike fashion, use the foreach scripting keyword:

PS >$myArray = 1,2,3 PS >$sum = 0 PS >foreach($element in $myArray) { $sum += $element } PS >$sum 6

To access items in an array by position, use a for loop:

Access Elements of an Array

Problem

You want to access the elements of an array.

Solution

To access a specific element of an array, use PowerShell’s array access mechanism:

PS >$myArray = 1,2,"Hello World" PS >$myArray[1] 2

To access a range of array elements, use array ranges and array slicing:

PS >$myArray = 1,2,"Hello World" PS >$myArray[1..2 + 0] 2 Hello World 1

Discussion

Create an Array or List of Items

Problem

You want to create an array or list of items.

Solution

To create an array that holds a given set of items, separate those items with commas:

PS >$myArray = 1,2,"Hello World" PS >$myArray 1 2 Hello World

To create an array of a specific size, use the NewObject cmdlet:

PS >$myArray = NewObject string[] 10 PS >$myArray[5] = "Hello" PS >$myArray[5] Hello

To store the output of a command that generates a list, use variable assignment:

PS >$myArray = GetProcess PS >$myArray

Pages

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Processor Intel Atom C2350 1.7 GHz 2c/2t RAM 4 GB DDR3 Storage 1× 1 TB (HDD SATA) Server Detail Configure Server
Processor Intel Atom C2350 1.7 GHz 2c/2t RAM 4 GB DDR3 Storage 1× 128 GB (SSD SATA) Server Detail Configure Server
Processor Intel Atom C2750 2.4 GHz 8c/8t RAM 8 GB DDR3 Storage 1× 1 TB (HDD SATA) Server Detail Configure Server
Processor Intel Xeon E3-1230 v2 3.3 GHz 4c/8t RAM 16 GB DDR3 Storage 1× 256 GB (SSD SATA) Server Detail Configure Server
Processor Intel Atom C2350 1.7 GHz 2c/2t RAM 4 GB DDR3 Storage 1× 250 GB (SSD SATA) Server Detail Configure Server

What Our Clients Say