Home > Mobile >  Powershell GUI How can I add column to GridView
Powershell GUI How can I add column to GridView

Time:01-14

This is part of a much larger script 1443 lines to be exact. it pulls the username from AD based on first and last name. I need to also have it pull the Office name from AD to help better identify users with same name. I am sure I am just missing something simple.

function getacctname {
    $fname = $FirstName.Text
    $lname = $LastName.Text
    Try {
     $User.Text = Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" |
        Select-Object -ExpandProperty 'SamAccountName' |
           Out-Gridview -Title 'Windows Logon' -PassThru
           $Email.Text = (Get-ADUser $User.text -Properties mail).mail
}

CodePudding user response:

Out-GridView dynamically builds its columns based on the input data you feed to it - so in order to get 3 columns, create an object with 3 properties!

Change the Select-Object statement so that it creates an object with properties corresponding to your desired columns and Out-GridView takes care of the rest:

Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" -Properties physicalDeliveryOfficeName |
     Select-Object 'SamAccountName',@{Name='Office';Expression={$_.physicalDeliveryOfficeName}} |
     Out-Gridview -Title 'Windows Logon' -PassThru

If the office name is stored in a different attribute, replace the two occurrances of physicalDeliveryOfficeName with the ldap display name of the attribute in question

  • Related