Techibee.com

Reversing a array using PowerShell

Array is a collection of items. Sometimes we needed to reverse or inverse a array collection. There are many approaches available in PowerShell for doing the reverse or inverse operations but the approach talked in this article is most easiest one.

Reversing String Array using PowerShell

The [Array] accelerator in PowerShell has a static method called reverse() which can be used to reverse a array. This method really doesn’t distinguish whether it is string array, number array or any other kind of array. All it does is reversing it. Though array type is not a matter, I have given a examples for each array type below for better understanding.

$StringArray = @(“abc”,“xyz”,“thn”)

$StringArray

[array]::Reverse($StringArray)

$StringArray

Reversing String Array

Reversing numeric array using PowerShell

Below is a example for reversing a numeric array using PowerShell.

$numbericarray = @(1,2,4,3,6,9,7)

$numbericarray

[array]::Reverse($numbericarray)

$numbericarray

Reversing Numeric array

Reversing Characters array using PowerShell

Below example is for reversing a characters array. Approach is similar to reversing number or string based arrays.

$chararray = @(‘a’,‘c’,‘g’,‘d’,‘r’)

$chararray

[array]::Reverse($chararray)

$chararray

 

Reversing Char array

Hope this article helps.

 

 

Exit mobile version