0

I've been stuck on this for a little while however I've got an array of People and im trying to get the last person and creating a seperate column with that person only.

I've played around with @{NAME = 'NAME' Expression = {}} in Select-Object but I don't really know how to tackle it.

Current:

| Employee      |
|---------------|
| John Doe      |
| Jane West     |
| Jordan Row    |
| Paul Willson  |
| Andrew Wright |

Desired Result:

| Employee     | Employee2     |
|--------------|---------------|
| John Doe     |               |
| Jane West    |               |
| Jordan Row   |               |
| Paul Willson | Andrew Wright |

TIA!

2
  • That's a strange requirement, but okay :) When you say 2nd column, you want an object, the last object, to have an Employee property = 'Paul Willson' and another property called Employee2 = 'Andrew Wright', but all the other values should only have only an Employee property? Or when you say columns are you looking mainly to have this output in csv? Commented Jun 16, 2021 at 5:38
  • @Daniel that first part is exactly what I want! :) Commented Jun 16, 2021 at 5:49

1 Answer 1

2

So what I decided to do here is create 2 groups. One group contains all of the values except the last 2, and the other group contains these last 2 values

# create the sample array
$employees = @(
    'John Doe'
    'Jane West'
    'Jordan Row'
    'Paul Willson'
    'Andrew Wright'
)

$employees | 
# Separate objects into 2 groups: those contained in the last 2 values and those not contained in the last 2 values
Group-Object {$_ -in ($employees | Select-Object -Last 2)} | 
    ForEach-Object {
        switch ($_) {
            {$_.name -eq 'False'} {  # 'False' Name of group where values are not one of the last 2

                # Iterate through all the values and assign them to Employee property.  Leave Employee2 property blank
                $_.group | ForEach-Object {
                    [PSCustomObject]@{
                        Employee  = $_
                        Employee2 = ''
                    }
                }
            }
            {$_.name -eq 'True'} {  # 'True' Name of group where values are those of the last 2
                # Create an object that assigns the values to Employee and Employee2
                [PSCustomObject]@{
                    Employee  = $_.group[0]
                    Employee2 = $_.group[1]
                }
            }
        }
    }

Output

Employee     Employee2
--------     ---------
John Doe
Jane West
Jordan Row
Paul Willson Andrew Wright

Edit

Here is another way you can do it

$employees[0..($employees.Count-3)] | ForEach-Object {
    [PSCustomObject]@{
        Employee = $_
        Employee2 = ''
    }

}

[PSCustomObject]@{
    Employee = $employees[-2]
    Employee2 = $employees[-1]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.