2

Is there an easier way to accomplish this without typing the series out? I tried some ideas with arrays and dereferencing, but nothing ended up working.

$rowP = "" | Select-Object Course,"5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35"

1 Answer 1

2

I prefer the following approach.

  1. Create an ordered dictionary:
$properties = [ordered]@{
  Course = $null
}
  1. Use the .. range operator to generate the range of integers 5 through 35, add empty entries to the dictionary:
5..35 |ForEach-Object {
  $properties["${_}"] = $null
}
  1. Convert the dictionary to a PSCustomObject:
$object = [pscustomobject]$properties

Alternatively, you could also use the range operator to generate the integer range and pass those as property names to Select-Object:

"" |Select-Object -Property @('Course'; 5..35 -as [string[]])
Sign up to request clarification or add additional context in comments.

2 Comments

"" |Select-Object -Property @('Course'; 5..35 -as [string[]]) This is what I was experimenting with but I was attempting to cast 5..35, like Course,[string[]] (5..35) and did not use the "Course"; at all. Thanks, this give good insight into where I was going wrong.
@BrianMcMahon You're welcome! Bareword arguments (strings without quotation marks) only work in argument mode (when you're passing a literal argument to a command basically). Check out the about_Parsing help topic for more details :)

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.