Questions tagged [psobject]

PSObject is used for object encapsulation in PowerShell and is used for a consistent view in its environment.

174 questions
58
votes
7 answers

Difference between PSObject, Hashtable, and PSCustomObject

Can anybody explain the details? If I create an object using $var = [PSObject]@{a=1;b=2;c=3} and then I look for its type using getType() PowerShell tells me it's of type Hashtable. When using Get-Member (alias gm) to inspect the object it's…
omni
  • 4,104
  • 8
  • 48
  • 67
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
16
votes
2 answers

Access PSObject property by name in C#

For example I have a PSObject transaction with two properties: id and transactionName , so that it looks like: transaction { id: 123 transactionName : tranName1 } and I want to return the id of the transaction if its name is tranName1. It…
jamesdeath123
  • 4,268
  • 11
  • 52
  • 93
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
10
votes
3 answers

Deep copying a PSObject

I have a powershell script in which I do the following $somePSObjectHashtables = New-Object Hashtable[] $somePSObject.Length; $somePSObjects = Import-CSV $csvPath 0..($somePSObject.Length - 1) | ForEach-Object { $i = $_; …
Justin Dearing
  • 14,270
  • 22
  • 88
  • 161
10
votes
2 answers

Passing "native" object to background jobs

Here is what I'd like to achieve in one way or another. I have a custom assembly defining some objects. In my script, I create a custom object that I'd like to pass to a script block, keeping that object behavior. Add-Type -AssemblyName…
David Brabant
  • 41,623
  • 16
  • 83
  • 111
9
votes
2 answers

Powershell - how do I edit an existing property in a custom object

I looking for way how update noteproperty in existing psobject, for example I have system.array of psobjects ($a): Group Assigment ----- --------- Group1 …
idMaxCZ
  • 93
  • 1
  • 1
  • 5
9
votes
5 answers

PowerShell: Custom Property XML Tags with ConvertTo-XML Output

I am creating a new object in PowerShell, using a hash table to set property values. I want to then export the object into XML format using the ConvertTo-XML method. $hash = @{ Processor = 'Intel' Disk = '500GB' …
corneria
  • 608
  • 3
  • 11
  • 24
6
votes
1 answer

Property passed to Invoke-Command changes type from IDictionary to HashTable

I've been getting an error running Invoke-Command where the script block takes a parameter of type dictionary: Cannot process argument transformation on parameter 'dictionary'. Cannot convert the "System.Collections.Hashtable" value of type …
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
5
votes
2 answers

Why does Get-Date seem to return DateTime objects, but the BinarySerializer indicate it returns a PSObject?

Take the simple HashTable: $data = @{ First = 'Justin'; Last = 'Dearing'; StartDate = Get-Date '2002-03-23'; } The key StartDate seems to contain a DateTime. C:\Users\zippy\Documents>…
Justin Dearing
  • 14,270
  • 22
  • 88
  • 161
5
votes
4 answers

Return Powershell PSObject as DataTable in C#

I've written a Powershell script that reads a CSV file and returns a predetermined collection from the data. Below is a sample output of said script. Count Name ------ ------ 12 Rubies 3 Pearls 20 Emeralds I am able to…
Yad
  • 229
  • 1
  • 8
  • 19
4
votes
3 answers

Method invocation failed op_addition

in this script i am pulling info from my SQL servers. i create the psobject and then i attempt to += each object into the $results table so that i can get a complete final report. for some reason if i run the script all the way through it gives me…
Jason
  • 41
  • 1
  • 1
  • 2
4
votes
2 answers

PSObject Array of Arrays Return Powershell Read Individual Items/Rows

I am using one Function to call another and returning a PSObject array or multiple arrays (I think). The return after each looped object is a set of values. The data in $a in Function Test2 is below but it's count is 3, meaning three objects or…
Nakious
  • 43
  • 3
4
votes
1 answer

Change value of a psobject property

I have an array full of psobjects. Now I want to change some properties in each of the objects in the array. My code: [array] $objectArray = $null foreach ($row in $result) { $object = New-Object -TypeName PSObject $object | Add-Member…
TeemoBiceps
  • 386
  • 1
  • 6
  • 22
4
votes
2 answers

Unexected results in Receive-Job

I noticed weird behavior when using jobs in PowerShell 5.0. Running job that returns PSObject, also returns some hashtable. Jobs that return strings, integers, etc work propertly. Running Start-Job { New-Object PSObject -Property @{ A = 1 } } | …
ernestasju
  • 1,319
  • 1
  • 11
  • 14
1
2 3
11 12