I have a PowerShell script that calls a function and sets the results of the function to a variable. Within the function, I am using the $variable | ft to display the contents of a variable in a table and asking the user to select a number that corresponds to the row in the table they want to act on. I am then returning this variable to the object that called the function:
function getusers{
$users = @()
$i = 0
do {
$user = ""| select Row, Username, Firstname, lastname
$user.row = $i
$user.username ="user$i"
$user.Firstname = "fname$i"
$user.lastname = "lname$i"
$users += $user
$i += 1
}while ($i -le 5)
# Actual logic to build the contents of the $users variable here.
$users | ft -AutoSize
$selection = Read-Host "Select number for appropriate user"
# Logic to determine if user selection is a valid number based on the number of rows in the variable.
$user = $users[$selection]
$user
}
$selecteduser = getusers
When calling the function and storing the results in a variable the $users | ft does not display in my console. If I just call the function without storing the results in a variable, the console displays the results of $users | ft.
Out-Gridviewwith the-passthruparameter instead ofFormat-Tablereturn $useras the last command in the function?returnstatements in PowerShell. Any value that is not assigned to a variable is returned.