-1

A simple question but how to make it happen eludes me.

I created a new object so I can have custom columns in Out-GridView. My question is, in the code the order is DN, Role, Montage, Lieu ... but in the actual GridView, it appears as IP, Montage, Role, ...

How do I set the order of appearance so it fits with how to code is written?

*Using powershell v5.0

if ($user1)
    {
        $Object += New-Object PSObject -Property @{
            DN          = $OU;
            Role        = $role;
            Montage     = $datemontage;
            Lieu        = $lieu;
            IP          = $IPAddress;
            MAC         = $MACAddress3;
            Modèle1     = $modele1;
            Modèle2     = $modele2;
            Login       = $temps2;
            User        = $user2;
            InfoUser    = $info_user2;
        }


    }
    else
    {

        $Object += New-Object PSObject -Property @{
            DN     = $OU;
            Role     = $role;
            Montage  = $datemontage;
            Lieu     = $lieu;
            IP     = $IPAddress;
            MAC   = $MACAddress3;
            Modèle1  = $modele1;
            Modèle2  = $modele2;
            User    = "AUCUN";
        }



    }

enter image description here

Rakha
  • 1,874
  • 3
  • 26
  • 60
  • 4
    Screenshot of `Out-GridView` is fine, but please [paste/edit the actual code into your question](https://stackoverflow.com/posts/48885998/edit) rather than a screenshot of notepad. Also, please include the version of PowerShell you are using – Mathias R. Jessen Feb 20 '18 at 13:13
  • Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Clijsters Feb 20 '18 at 13:28
  • Apologies for the screenshot, i thought it would cause such concerns but the code was in a VM at the time of writing. Here it is included now. – Rakha Feb 20 '18 at 13:39

1 Answers1

2

If you are creating the object from hashtable, in Powershell v3 and later you can use [ordered] type adapter:

$h = [ordered]@{B="First";A="Second"}
New-Object psobject -property $h
Janne Tuukkanen
  • 1,620
  • 9
  • 13