Out-GridView Column Name

I wrote a script that our help desk uses to collect network data from different sites. The results are logged to disk and also sent via email using an Outlook com object. It works great and quickly provides accurate data to engineers that trouble tickets are escalated to.
I have the help desk tech select a site from a list. I have a $site variable that is an array of each individual site inclusive of both site code and site name like “001 - Site 1”, “002 - Site 2”, etc. I pipe the selection out using Out-GridView -OutputMode Single. The one annoyance is the column name for the selected site is “string”.
So I edited the $site array to be a custom psobject for each site using the following:
[pscustomobject]@{‘Site Code’=‘001’;‘Site Name’=‘Site 1’}
[pscustomobject]@{‘Site Code’=‘002’;‘Site Name’=‘Site 2’}
Now I have the desired column names (Site Code, Site Name) but when I reference the $site variable for log title, email subject, etc. I get the following:
@{Site Code=001; Site Name=Site 1}
Any help with this would be greatly appreciated. I would like both the correct column names as well as the log titles, email subject, etc. to be simply “001 - Site 1” without the unnecessary formatting that resulted from switching to using a pscustomobject.
Thank you!
-John

Hi, welcome to the forum :wave:

Firstly, when posting code in the forum, please can you use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

How to format code on PowerShell.org

You can access the individual properties of your object using dot notation:

foreach ($site in $siteList) {
    Write-Output "The site code is $($site.'Site Code')"
    Write-Output "The site name is $($site.'Site Name')"
}

Note the use of the subexpression operator $() which is required when using dot notation in a string.

1 Like