Retrieve items from an Array

I’m new to Powershell ISE. Any advice is greatly appreciated. Script runs but not correctly. No matter what name you input, the age output is always 45.

[String] $name = @(“John Morales”, “Joe Root”, “Peter Barkley”)
[int16] $age = @(45, 67, 31)
$user = (Read-Host(“Please enter your name”))
$val = $age[0] ## This part I know is wrong…not sure how to make it work.
$ndx = [array]::IndexOf($name, $age);
if ($user -in $name)
{

Write-Host $(“{0,-15} {1,0}” -f “Name”, “Age”)
Write-Host $(“{0,-15} {1,0}” -f “------”, “—”)
Write-Host $(“{0,-15} {1,0}” -f “$user”, “$val”)

}
else
{
Write-Host “User not found”

}

J,

Welcome to the forum. :wave:t4:

There are a few issues with your code. First of all - when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

In PowerShell (and in most other scripting or programming languages) the index of an array starts with 0. So With the following line of code you explicitly select the first element of the array with the name $age. And that’s always 45

$val = $age[0] ## This part I know is wrong…not sure how to make it work.

The next line of code is useless because you never use the variable you define.

$ndx = [array]::IndexOf($name, $age);

Regardless of that are your arrays actually completely unrelated to each other.

If I had to assume what you wanted to achieve I’d correct your code to this:

[String[]] $name = 'John Morales', 'Joe Root', 'Peter Barkley'
[int16[]] $age = 45, 67, 31
$user = Read-Host -Prompt 'Please enter your name'
$ndx = $name.IndexOf($user)
$val = $age[$ndx]
if ($user -in $name) {
    '{0,-15} {1,0}' -f 'Name', 'Age'
    '{0,-15} {1,0}' -f '------', '---'
    '{0,-15} {1,0}' -f $user, $val
}
else {
    'User not found'
}

But that’s actually not how we write PowerShell code. :wink: To provide your input where the names are actually related to their ages you use in the simplest case CSV data.
Something like this would work:

$CSVInput =
@'
Name,Age
"John Morales",45
"Joe Root",67
"Peter Barkley",31
'@ | 
    ConvertFrom-Csv 
$UserName = Read-Host -Prompt 'Please enter your name'

$Result= 
    $CSVInput | 
        Where-Object -Property Name -EQ -Value $UserName

if ($Result) {
    $Result
}
else{
    'User not found'
}

Instead of providing the input data along with your code you would normaly use a CSV file and the cmdlet Import-Csv to get the data.

Thank you so much for the explanation. These are the examples I was following in my book.

#file : ps_section15_example_12_b.ps1
#PowerShell constructs : Finding Element position in an Array using [array]::indexOf method

[int16[]] $numbers = (456, 111, 413, 456, 187)
[int16] $inNumber = Read-Host "Enter Number to Search"
$foundPos = [array]::indexof($numbers,$inNumber);
if ($foundPos -gt -1){
Write-Host "Yes the number $inNumber found in position $foundPos."
}else{
Write-Host "Sorry the number $inNumber does not exist."
} 


#Filling Arrays with data sets from a file
#file : ps_section15_example_16.ps1

$file = Get-Content "persons.dat"

$line = $null;

[String[]] $name = @();

[int16[]] $age = @();

[float[]] $salary = @();

foreach ($line in $file)

{ #Split fields into values

$line = $line -split (",")

$name += $line[0];

$age += $line[1];

$salary += $line[2];

}

Write-Host $("{0,-20} {1,7} {2,11}" -f "Name",

"Age", "Salary")

Write-Host $("{0,-20} {1,7} {2,11}" -f "-----------", "---", "-----------")for ($nextItem=0 ; $nextItem -lt

$name.length; $nextItem++)

{

$val1n = $name[$nextItem];

$val2n = $age[$nextItem]

$val3n = $salary[$nextItem]

Write-Host $("{0,-20} {1,7} {2,11:n2}" -f $val1n, $val2n, $val3n)

}

This was saved as person.dat
George Nelson,56,78000.00
Mary Nathaniel,65,66300.00
Rosy Ferreira,32,39000.00

OK. While this might be technically correct I’d consider this as unnecessarily complex and less usual in real world. There are a few techniques they are actually considered bad style.
May I ask what book this is?

You may read the following as a follow up: :wink:

My mistake. It was actually a pdf titled " Scripting Languages PowerShell for Windows" containing multiple samples of scripts. It is what was provided by my school and is very vague. My next objective with this script is to put the names and ages into a dat file and find a way to retrieve and print just one user with their age. Thanks for providing the link.

That’s what I actually meant in my first answer. We do not use something like “dat” files. For structured data like this you have mostly CSV files or you retrieve data from a data source like an Active Directory for example. There you have a unique relationship between the data fields of a record. That’s what I tried to show with my code suggestion in my first answer.

Reading your code makes alot more sense. It’s unfortunate that I have no choice but to follow the instructions given to me.