Need Help Select-Object Argument

How do I build the select statement argument so I can call select dynamically?

When I reference my variable in the select statement it concatenates into a single string - (expansion?)
If I call the variable spearately then copy and paste the output of the variable I get what I need.


$import = import-csv 'C:\Users\NickCrooks\OneDrive - xyzworkplace\roleproject\20200723_MasterPermissionsList.csv' #$env:userprofile\downloads\rolepermissiondifferences149.csv #MasterRoleComparison
$users = Import-Csv 'C:\Users\NickCrooks\OneDrive - xyzworkplace\roleproject\SearchExports\20200723_userimport.csv' #$env:userprofile\Downloads\NJCEmployeeSearchResults329.csv #roleNotNull_users
$titlehash = $users | ?{$_.role -notin ('System Admin - view only','Integrations')} | select role, name | Group-Object -Property name -AsHashTable
#$ref = $import | select Category, Permission

#region operation
foreach($key in $titlehash.keys) {
$val = $titlehash[$key]
$sb = [System.Text.StringBuilder]::new()
#https://powershellexplained.com/2017-11-20-Powershell-StringBuilder/

if($val.name.GetType() -match 'System.object') {
#$Title = New-Object psobject
#$Title | Add-Member -MemberType NoteProperty -Name "$($val.name[0])" -Value $ref #moved this
#Write-host $val.name[0] -ForegroundColor yellow
#$val."role" | select -Unique
write-output $roles.count -Verbose

#write-verbose "$roles found" -Verbose

$val | %{
[string]$role = $_.role
#$role
$sb.Append('"'+"$role"+'",')
}
$list = $sb.ToString() #Need to trim the last comma off, so I need substring method
$list.Substring(0,($sb.Length)-1)
$import | select -Property $($list.Substring(0,($sb.Length)-1)) | Out-GridView
#Here's the output of $list.Substring(0,($sb.Length)-1) - and it works if I copy & paste the text below
#"Candidate Relations","Practice Director","Candidate Relations - TAF","Human Resources Assistant","Outlook -App","Permission"
pause
}
else {
#Single values whose type is different
write-host $val.name.ToString() -ForegroundColor Red
$val."role"
$import | select permission, $val.role | Export-csv "C:\Users\NickCrooks\Desktop\role\$($val.name).csv" -force
#pause
}
}

$($list.Substring(0,($sb.Length)-1))

Above expressions gives a string here which has commas and treated whole as a property name to select. So here you just need to split it by comma which will give you an array and each item in the array will be treated as property to select as -Property will accept array of strings.

($list.Substring(0,($sb.Length)-1) -split ',')

So close!

Thanks for the response kvprasoon!

Unfortunately I get empty properties/columns using this method - columns are correct but no data whereas if I type it manually I get data back

Here’s what I see

$import | select -Property ($list.Substring(0,($sb.Length)-1) -split ',')

"Candidate Relations" :
"Practice Director" :
"Candidate Relations - TAF" :
"Human Resources Assistant" :
"Outlook - NetSuite" :

"Candidate Relations" :
"Practice Director" :
"Candidate Relations - TAF" :
"Human Resources Assistant" :
"Outlook - NetSuite" :

Here’s what I was hoping for:


$list.Substring(0,($sb.Length)-1)
"Candidate Relations","Practice Director","Candidate Relations - TAF","Human Resources Assistant","Outlook - NetSuite"

PS C:\> $import | select "Candidate Relations","Practice Director","Candidate Relations - TAF","Human Resources Assistant","Outlook - NetSuite"

Candidate Relations       : None
Practice Director         : None
Candidate Relations - TAF : None
Human Resources Assistant : None
Outlook - NetSuite        : None

Candidate Relations       : None
Practice Director         : None
Candidate Relations - TAF : None
Human Resources Assistant : None
Outlook - NetSuite        : None

Candidate Relations       : None
Practice Director         : None
Candidate Relations - TAF : None
Human Resources Assistant : None
Outlook - NetSuite        : None

Candidate Relations       : None
Practice Director         : None
Candidate Relations - TAF : None
Human Resources Assistant : None
Outlook - NetSuite        : None

Candidate Relations       : None
Practice Director         : None
Candidate Relations - TAF : None
Human Resources Assistant : None
Outlook - NetSuite        : None

Candidate Relations       : None
Practice Director         : None
Candidate Relations - TAF : None
Human Resources Assistant : None
Outlook - NetSuite        : None

Candidate Relations       : None
Practice Director         : None
Candidate Relations - TAF : None
Human Resources Assistant : None
Outlook - NetSuite        : None

Candidate Relations       : None
Practice Director         : None
Candidate Relations - TAF : None
Human Resources Assistant : None
Outlook - NetSuite        : None

Candidate Relations       : Full
Practice Director         : Full
Candidate Relations - TAF : Full
Human Resources Assistant : None
Outlook - NetSuite        : None

$import | select -Property ($list.Substring(0,($sb.Length)-1) -split ',').replace('"','')

This is it!!

Why does that work, when if typing it out I would need to add the quotes?

Why are you collecting the role names like this?

Most natural way for me to do so/Lack of experience doing it other ways :smiley:

Huge thanks to everyone here for all the knowledge/help.

As far as Netsuite (system it comes from) the output is a csv, where properties are (first two are always the same)
Category,Permission,Role1,Role2,Role3, etc.

Separately I have a csv of users where their role(s) are included.

What did you have in mind, Doug?