Questions tagged [pscustomobject]

Shorthand syntax for initializing object properties in powershell

Docs: MSDN - PSCustomObject

Powershell v2.0

$object = New-Object -TypeName PSObject -Property @{  ​
    'Name' = 'Susan'
    'Age' = 32
}

Powershell v3.0

$object = [PSCustomObject]@{  ​
    'Name' = 'Susan'
    'Age' = 32
}

See also:

165 questions
90
votes
10 answers

PSCustomObject to Hashtable

What is the easiest way to convert a PSCustomObject to a Hashtable? It displays just like one with the splat operator, curly braces and what appear to be key value pairs. When I try to cast it to [Hashtable] it doesn't work. I also tried .toString()…
alphadev
  • 1,529
  • 5
  • 18
  • 20
78
votes
9 answers

How to initialize an array of custom objects

First, as this leads to my question, I'll start by noting that I've worked with XML a fair bit in PowerShell, and like how I can read data from XML files, quickly, into arrays of custom objects. For example, if I had the following XML file:
SteveDJ
  • 993
  • 2
  • 7
  • 10
48
votes
4 answers

How to get Select-Object to return a raw type (e.g. String) rather than PSCustomObject?

The following code gives me an array of PSCustomObjects, how can I get it to return an array of Strings? $files = Get-ChildItem $directory -Recurse | Select-Object FullName | Where-Object {!($_.psiscontainer)} (As a secondary question, what's the…
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
24
votes
2 answers

PowerShell - "Write-Output" vs "return" in functions

I've been using PowerShell for a number of years, and I thought I had a handle on some of its more 'eccentric' behaviour, but I've hit an issue I can't make head nor tail of... I've always used "return" to return values from functions, but recently…
mclayton
  • 8,025
  • 2
  • 21
  • 26
15
votes
2 answers

PowerShell type accelerators: PSObject vs PSCustomObject

In PowerShell v3.0 PSCustomObject was introduced. It's like PSObject, but better. Among other improvements (e.g. property order being preserved), creating object from hashtable is simplified: [PSCustomObject]@{one=1; two=2;} Now it seems obvious…
AdamL
  • 12,421
  • 5
  • 50
  • 74
8
votes
2 answers

How to add a custom property to a PowerShell array?

Say I have a PowerShell array $Sessions = @() which I am going to fill with PSCustomObjects. How can I add a custom property to the array itself? E.g. so I can have $Sessions.Count which is built-in and $Sessions.Active which I want to set to the…
FatalMerlin
  • 1,463
  • 2
  • 14
  • 25
6
votes
2 answers

PowerShell: Importing 16MB CSV Into PowerShell Variable Creates >600MB's of PowerShell Memory Usage

I'm trying to understand why PowerShell's memory balloons so much when I import a file that's ~16MB's as a variable. I can understand there's additional memory structure around that variable but I'm just trying to understand why it's THAT high.…
6
votes
1 answer

Powershell Custom Objects: How To Acess NoteProperty of collected result

Reading through an Article about custom Objects: http://technet.microsoft.com/en-us/library/ff730946.aspx I wonder why i get a result for the individual entry: e.g $objAverage.Name while by $colAverages.Name nothing is returened. Although with…
icnivad
  • 2,231
  • 8
  • 29
  • 35
5
votes
3 answers

How to convert PSCustomObject with additional props to a custom class

Is there a neat way to convert a PSCustomObject to a custom class as a function parameter in PowerShell 5.1? The custom object contains additional properties. I'd like to be able to do something like this: class MyClass { …
Grzegorz Smulko
  • 2,525
  • 1
  • 29
  • 42
4
votes
1 answer

In an PSCustomObject, is it possible to refer an item belonging to it inside itself?

I need to keep inside a PSCustomObject, values at 2 or 3 places and these places are different depending of the object created. The object are hard coded in the script but sometime I need to change the values. But instead of change theses value at 2…
DDD
  • 43
  • 4
4
votes
3 answers

Prevent adding pscustomobject to array if already exists

I feel silly that I cannot figure this out, but say I have an array containing pscustomobjects. At an incredibly high level take the following example: $arr = @() $obj1 = [pscustomobject]@{prop1="bob";prop2="dude";prop3="awesome"} $obj2 =…
4
votes
3 answers

Powershell Selecting NoteProperty Type Objects From Object

I am working with deeply nested JSON and, after convertfrom-json, need to be able to traverse through various parts of the object which the convertfrom-json cmdlet generates. I have no way of knowing in advance what property names may or may not be…
Indrid
  • 962
  • 4
  • 25
  • 39
4
votes
1 answer

ExpandProperty - objects missing from pipeline

I have some objects in a pipeline, something like this: $arr1 = @( (New-Object -TypeName psobject -Property @{'objname'='obj1';props=@((new-object -typename psobject -Property @{'pname'='prop1';'val'=11;}),(new-object -typename psobject…
AdamL
  • 12,421
  • 5
  • 50
  • 74
3
votes
3 answers

How do I convert a powershell hashtable to an object?

Some hashtables in PowerShell, such as those imported with Import-PowerShellDataFile, would be much easier to navigate if being a PSCustomObject instead. @{ AllNodes = @( @{ NodeName = 'SRV1' Role = 'Application' …
Dennis
  • 871
  • 9
  • 29
3
votes
3 answers

How to simplify multidimension array using Powershell?

I would like to create multidimension array in powershell, I tried this way and it works, but I want to simplified it to make it there is no repetition of name and pn. Anyone can give idea please, Thanks a lot $array = @( [PSCustomObject]@{name…
Cheries
  • 834
  • 1
  • 13
  • 32
1
2 3
10 11