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
2
votes
2 answers

Copying Powershell PSCustomObject to array element will pass reference, not value

Not sure what I am doing wrong, but when copying a PSCustomObject in Powershell to an array element, this is copied by reference. See this: $body = [PSCustomObject]@{ albumId = $album_id; newMediaItems = [PSCustomObject]@() }…
Riccardo
  • 2,054
  • 6
  • 33
  • 51
2
votes
1 answer

Access Array of object inside a PSCustomObject Powershell

I have this PSCustomObject which I'm trying to format into a table and export it to an .csv file, when the .csv file is built, the output in the cell is System.Object[]. $tableFindings = @() foreach ($item in $findings.results) { $tabledata =…
Bryan Arreola
  • 197
  • 1
  • 6
  • 22
2
votes
2 answers

Why are script properties lost when passing pscustomobject to start-job script block?

On windows XP x64 (and I assume win2k3) powershell 2.0, passing an arraylist of pscustomobjects to start-job as argumentlist parameter passes the object in but scriptproperties just disappear from the object (confirmed by get-member). Note…
JorgeSandoval
  • 1,085
  • 1
  • 10
  • 20
2
votes
3 answers

How to access powershell PSCustomObject variable with variable (from json)

I have a PSCustomObject which has list of sub-objects like this: vmssSystemUpdatesMonitoringEffect : @{type=String; metadata=; allowedValues=System.Object[];…
Kamsiinov
  • 1,315
  • 2
  • 20
  • 50
2
votes
2 answers

How to use powershell to determine the frequency of objects in a collection based on a specific member

I am query the Security EventLog on our PDC to watch for trends that might indicate compromised hosts or usernames. I have got the code to gather the info and clean it up... $TargetEvents ends up like this: (I can't figure out how to format a…
2
votes
2 answers

Access multiple outputs from a script by symbolic names (keys)

I'm a beginner in powershell scripting, I want to be able to distinguish between the outputs of a script. Lets take as an example this script test.ps1: param([System.String] $w) $x=$w+" is correct" $y=$w+ " is false" $x $y to execute it and…
user3146966
  • 165
  • 1
  • 1
  • 6
2
votes
1 answer

PowerShell conditional statements while building PSCustomObject

I want to perform a check of a variable's existence while creating a PSCustomObject. I have quite a few objects to query and gather data for into my new object so I don't want to duplicate the entire code block with an "if" statement as I'm trying…
Jason Shave
  • 2,462
  • 2
  • 29
  • 47
2
votes
3 answers

How to avoid leading white space addition with string formatting?

I have a function which returns PSCustomObject, like this: Function Get-Data { # ... [PSCustomObject]@{ Url = $Url Id = $Id } } Later on, I call this function like this: $data = Get-Data And then I'd like to output formatted string…
Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
2
votes
2 answers

Powershell create object from multiple files

I am making a PSObject from a json file bar.json { "derp": { "buzz": 42 }, "woot": { "toot": 9000 } } I can make a PSCustomObject form the json using ConvertFrom-Json $foo = Get-Content .\bar.json -Raw…
spuder
  • 17,437
  • 19
  • 87
  • 153
2
votes
1 answer

Error adding second type that is dependent on the first

I'm writing a script using PowerShell which uses a custom object. But the tricky part is that this custom object uses another custom object as a property. I'm trying to do it with C# syntax. When I'm trying to run the script I'm getting this…
SokIsKedu
  • 196
  • 1
  • 2
  • 17
2
votes
1 answer

Powershell Object not being piped through to Functions

I have two functions one creates a custom object which once done is piped to the next function. The problem is that the second function is not receiving my object correctly. Instead of using the pipeline I have tried setting a variable and then…
fauxops
  • 63
  • 6
2
votes
1 answer

Change values of an array of pscustomobject's with a for-loop

I have tried to create a array of custom objects (pscustomobject) and now i tried to change some values of the different custom objects with an for-loop. But it doesn't seem to work. Here is what i tried: $obj =…
frupfrup
  • 187
  • 2
  • 12
1
vote
2 answers

Merge PowerShell Array in to PSCustomObject (JSON)

I am working to built a function to construct a json payload for an API call. The json payload will have a main "member", but I need to add a sub-member which is really a json array. I have this almost working, but I have been unable to get it…
James
  • 75
  • 2
  • 8
1
vote
1 answer

Access Nested PCustomObject Array Property

$personData = @' { "persons": [{ "johndoe": [{ "id": "4334234 <>", "family_adress": { "mother": "address of family", "father": "", "brother": "address of family" }, …
Ichigo Kurosaki
  • 135
  • 1
  • 6
1
vote
1 answer

How to replace only specific strings in PowerShell custom object?

This is the base code: foreach ($event in Get-WinEvent -FilterHashtable @{LogName='Security';ID=5152}) { $xml = [xml]$event.toxml(); $xml.event.eventdata.data | foreach { $hash = @{} } { $hash[$_.name] = $_.'#text' } {…
user20682592
1 2
3
10 11