r/PowerShell • u/AardvarkNo8869 • Dec 06 '25
I HATE PSCustomObjects
Sorry, I just don't get it. They're an imbred version of the Hashtable. You can't access them via index notation, you can't work with them where identity matters because two PSCustomObjects have the same hashcodes, and every variable is a PSCustomObjects making type checking harder when working with PSCO's over Hashtables.
They also do this weird thing where they wrap around a literal value, so if you convert literal values from JSON, you have a situation where .GetType() on a number (or any literal value) shows up as a PSCustomObject rather than as Int32.
Literally what justifies their existence.
Implementation for table:
$a = @{one=1;two=2; three=3}
[String]$tableString = ""
[String]$indent = " "
[String]$seperator = "-"
$lengths = [System.Collections.ArrayList]@()
function Add-Element {
param (
[Parameter(Mandatory)]
[Array]$elements,
[String]$indent = " "
)
process {
for ($i=0; $i -lt $Lengths.Count; $i++) {
[String]$elem = $elements[$i]
[Int]$max = $lengths[$i]
[String]$whiteSpace = $indent + " " * ($max - $elem.Length)
$Script:tableString += $elem
$Script:tableString += $whiteSpace
}
}
}
$keys = [Object[]]$a.keys
$values = [Object[]]$a.values
for ($i=0; $i -lt $keys.Count; $i++) {
[String]$key = $keys[$i]
[String]$value = $values[$i]
$lengths.add([Math]::Max($key.Length, $value.Length)) | Out-Null
}
Add-Element $keys
$tableString+="`n"
for ($i=0; $i -lt $Lengths.Count; $i++) {
[Int]$max = $lengths[$i]
[String]$whiteSpace = $seperator * $max + $indent
$tableString += $whiteSpace
}
$tableString+="`n"
Add-Element $values
$tableString
$a = @{one=1;two=2; three=3}
[String]$tableString = ""
[String]$indent = " "
[String]$seperator = "-"
$lengths = [System.Collections.ArrayList]@()
function Add-Element {
param (
[Parameter(Mandatory)]
[Array]$elements,
[String]$indent = " "
)
process {
for ($i=0; $i -lt $Lengths.Count; $i++) {
[String]$elem = $elements[$i]
[Int]$max = $lengths[$i]
[String]$whiteSpace = $indent + " " * ($max - $elem.Length)
$Script:tableString += $elem
$Script:tableString += $whiteSpace
}
}
}
$keys = [Object[]]$a.keys
$values = [Object[]]$a.values
for ($i=0; $i -lt $keys.Count; $i++) {
[String]$key = $keys[$i]
[String]$value = $values[$i]
$lengths.add([Math]::Max($key.Length, $value.Length)) | Out-Null
}
Add-Element $keys
$tableString+="`n"
for ($i=0; $i -lt $Lengths.Count; $i++) {
[Int]$max = $lengths[$i]
[String]$whiteSpace = $seperator * $max + $indent
$tableString += $whiteSpace
}
$tableString+="`n"
Add-Element $values
$tableString
View all comments
Show parent comment
0
u/AardvarkNo8869 Dec 06 '25
OK, I will actually take on this challenge and return to you with some code.