Question about hashtables

Hi!

Hi! I have a question and was wondering if someone care to explain some more on hashtables and the way i do this in the example, maybe some other ways to acomplish the task. Here goes:

This is the code: $HashTbl = =@{Machine=“PC1”;IP=“10.0.0.1”;User=“iktspesialisten}”}
The output result of this gives the first column Name and the next Value.
I was wondering how i can change this so it say something different, like Name and Information for example.

Hope i got to explain what i am trying to do in a way that you guys understand. I guess i dont have enough background information to quite grasp how this works :slight_smile:

There’s not really a neat way to do exactly what you’re asking.

However, this usually works well enough for most cases:

PS > [PSCustomObject]$Hashtbl

IP       User             Machine
--       ----             -------
10.0.0.1 iktspesialisten} PC1

 

If you just want to change the header row names, you can pipe to the format-table cmdlet. Example below (I added -autosize but not necessary).

 

$HashTbl  = @{Machine="PC1";IP="10.0.0.1";User="iktspesialisten}"}
$HashTbl | ft -Property @{n="Name";e={$_.name}}, @{n="Information";e={$_.value}} -AutoSize

Method suggested by Joel is the easy way to do it. Here the Hash table is transformed to a PSCustomObject. I would like to add one more point.

If you are running PowerShell version 2.0 typecasting to [PSCUstomObject] will not work. So I always recommend by creating a new instance of PSObject.

New-Object -TypeName PSObject -Property @{Machine="PC1";IP="10.0.0.1";User="iktspesialisten}"}

and you still want it like key value model, you can transform it using calculated properties like below.

(@{Machine="PC1";IP="10.0.0.1";User="iktspesialisten}"}).GetEnumerator()|Select-Object -Property Name,@{E={$_.Value};l='Information'}

Note: Using Format-* cmdlets will convert everything to text and you will loose the power of objects. Format-* cmdlets are used just for printing the output by applying user defined formatting.

Good pointers for the other folks…

See also:

https://kevinmarquette.github.io/2016-11-06-powershell-hashtable-everything-you-wanted-to-know-about
 

If you’re running v2.0, you’re gonna have a lot more troubles than that.

Additionally, always recommending the New-Object PSObject method is still a bad idea.

New-Object is very slow (on average about 8 times slower than ::new() in PS 5, though I think it’s gotten faster across the versions; it’s also much slower than the hashtable construction method of custom objects).

Depending on the exact method you’re employing, PSObject as a type can be very difficult to work with. As I recall, the hashtable construction method is available from v3.0 onwards and should be used unless you specifically have to support PS v2.0 or lower for some reason.

Hi again!

I was able to do New-Object -TypeName PSObject -Property @{Machine=“PC1”;IP=“10.0.0.1”;User=“iktspesialisten}”}

I was just wondering what is the name of the object now? And also, when i pipe this to get-member it say PSCustomObject and not PSObject. Is there any way to convert it to be a PSObject so i can use the properties and maybe hook up the Machine name to ByValue so i can pipe a list of computers in there?

Some clarification – everything in PowerShell is inherently a PSObject. It’s a wrapper type, doesn’t really mean all that much on its own. All objects you’re going to create directly, unless you build your own class to create them from, will be PSCustomObjects.

The latter part of your question doesn’t make a whole lot of sense. If you’re talking about taking pipeline input from a script or function, your framework would look like this:

param(
    [Parameter(ValueFromPipeline, Mandatory)]
    [Alias('Machine')]
    [ValidateNotNullOrEmpty()]
    [string[]]
    $ComputerName
)
# process block required for pipeline input; executes once for every item passed in
process {
    # loop allows direct array input as well, if desired
    foreach ($PC in $ComputerName) {
        [PSCustomObject]@{
            Machine = $PC
            IP = # however you want to retrieve the IP
            User = # whatever you need here
        }
    }
}