0

How do I code this to output the columns in this order:

unique_sis_group_id, unique_sis_user_id, unique_sis_school_id, mm_admin

My code:

$objs = @();
$output = Import-Csv -Path "c:\users\patrick\desktop\RelayFiles\RelayMemberships\file2concatenate.csv" | ForEach {
    $Object = New-Object PSObject -Property @{
        unique_sis_group_id  = [String]::Concat($_.unique_sis_group_id, $_.unique_sis_user_id)
        unique_sis_user_id = $_.unique_sis_school_id
        unique_sis_school_id = $_.mm_admin
        mm_admin = 0
    }
    $objs += $Object;
}
$objs
$objs | Export-Csv -NoTypeInformation c:\users\patrick\desktop\RelayFiles\RelayMemberships\memberships.csv
3
  • 1
    if you use either an [ordered] hashtable OR [better] the [PSCustomObject] type accelerator, you will get your props in the declared order. Commented Jan 26, 2019 at 0:01
  • @Lee_Dailey Note that both [ordered] and [PSCustomObject] are not available prior to PowerShell v3. Commented Jan 26, 2019 at 12:33
  • @AnsgarWiechers - ah! thank you for the reminder! [grin] Commented Jan 26, 2019 at 16:13

2 Answers 2

1

You assign the ForEach-Object to $output, but nothing inside actually outputs something. Using a [PSCustomObject] is much simpler:

## Q:\Test\2019\01\25\SO_54373962.ps1
$CsvIn  = "c:\users\patrick\desktop\RelayFiles\RelayMemberships\file2concatenate.csv"
$CsvOut = "c:\users\patrick\desktop\RelayFiles\RelayMemberships\memberships.csv"
$output = Import-Csv -Path $CsvIn | ForEach-Object {
    [PSCustomObject]@{
         unique_sis_group_id  = [String]::Concat($_.unique_sis_group_id, $_.unique_sis_user_id)
         unique_sis_user_id   = $_.unique_sis_school_id
         unique_sis_school_id = $_.mm_admin
         mm_admin             = 0
    }
}
$output
$output | Export-Csv $CsvOut -NoTypeInformation
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the lesson. Much appreciated!
0

This took care of it. Is there a better way?

$objs =@();
$output = Import-Csv -Path "c:\users\patrick\desktop\RelayFiles\RelayMemberships\file2concatenate.csv" | ForEach {
    $Object = New-Object PSObject -Property @{
        unique_sis_group_id  = [String]::Concat($_.unique_sis_group_id, $_.unique_sis_user_id)
        unique_sis_user_id = $_.unique_sis_school_id
        unique_sis_school_id = $_.mm_admin
        mm_admin = 0
    }
    $objs += $Object;
}
$objs |
    Select-Object "unique_sis_group_id", "unique_sis_user_id",
        "unique_sis_school_id", "mm_admin" |
    Export-CSv -NoTypeInformation c:\users\patrick\desktop\RelayFiles\RelayMemberships\memberships.csv

1 Comment

For PowerShell v2 and earlier Select-Object is the way to go, because the [ordered] and [PSCustomObject] type accelerators have been introduced with PowerShell v3 and aren't available in earlier versions. If you have PowerShell v3 or newer use the [PSCustomObject] type accelerator as described in this answer, which also avoids the pitfall of appending to an array in a loop.

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.