cfoster5
cfoster5

Reputation: 1826

Get last string from variable

Using PowerShell, I created a script that other people will be using. Because of this, I am refactoring my code to use more variables. I have a variable named $endcol that will always be equal to the last string placed in another variable, $columnsToExtract. How can I make this work?

Right now my code is:

$columnsToExtract = '30', '31', '39'
$endcol = 39

Upvotes: 0

Views: 488

Answers (2)

Olaf
Olaf

Reputation: 5232

To get always the last element of an array you can use the "negativ index" starting with -1. Like this:

$columnsToExtract = '30', '31', '39'
$endcol = $columnsToExtract[-1]

Upvotes: 1

DKU
DKU

Reputation: 75

$columntoextract = '30','31','39'
$endcol = $($columntoextract[2])
$endcol

Upvotes: -1

Related Questions