Printing Object Properties in Powershell

When working in an interactive console, if I define a new object and assign it some property values ​​as follows:

$obj = New-Object System.String $obj | Add-Member NoteProperty SomeProperty "Test" 

Then, when I enter the name of my variable in an interactive window, Powershell gives me a summary of the object's properties and values:

 PS C:\demo> $obj SomeProperty ------------ Test 

Basically I want to do just that, but from inside a function in a script. The function creates an object and sets some property values, and I want it to print a summary of the values ​​of the objects in the Powershell before returning. I tried using Write-Host inside the function:

 Write-Host $obj 

But this just infers the type of the object, not the summary:

 System.Object 

How can I get my function to display a summary of the properties of an object in a Powershell window?

+55
powershell
Apr 03 '13 at 16:17
source share
4 answers

Try the following:

 Write-Host ($obj | Format-Table | Out-String) 

or

 Write-Host ($obj | Format-List | Out-String) 
+95
Apr 03 '13 at 16:28
source share

My solution to this problem was to use the $ () block of the subexpression .

 Add-Type -Language CSharp @" public class Thing{ public string Name; } "@; $x = New-Object Thing $x.Name = "Bill" Write-Output "My name is $($x.Name)" Write-Output "This won't work right: $x.Name" 

gives:

 My name is Bill This won't work right: Thing.Name 
+12
Jun 18 '15 at 19:51
source share

To print the properties and values ​​of objects in Powershell. The below examples work well for me.

$ pool = Get-Item "IIS: \ AppPools.NET v4.5"

$ pool | Get-member

  TypeName: Microsoft.IIs.PowerShell.Framework.ConfigurationElement#system.applicationHost/applicationPools#add Name MemberType Definition ---- ---------- ---------- Recycle CodeMethod void Recycle() Start CodeMethod void Start() Stop CodeMethod void Stop() applicationPoolSid CodeProperty Microsoft.IIs.PowerShell.Framework.CodeProperty state CodeProperty Microsoft.IIs.PowerShell.Framework.CodeProperty ClearLocalData Method void ClearLocalData() Copy Method void Copy(Microsoft.IIs.PowerShell.Framework.ConfigurationElement ... Delete Method void Delete() ... 

$ pool | Select-Object -Property * # You can omit -Property

 name : .NET v4.5 queueLength : 1000 autoStart : True enable32BitAppOnWin64 : False managedRuntimeVersion : v4.0 managedRuntimeLoader : webengine4.dll enableConfigurationOverride : True managedPipelineMode : Integrated CLRConfigFile : passAnonymousToken : True startMode : OnDemand state : Started applicationPoolSid : S-1-5-82-271721585-897601226-2024613209-625570482-296978595 processModel : Microsoft.IIs.PowerShell.Framework.ConfigurationElement ... 
+7
Feb 25 '16 at 4:38
source share

Tips # 1 and # 12

Ideally, your script will create your objects ( $obj = New-Object -TypeName psobject -Property @{'SomeProperty'='Test'} ) and then just Write-Output $objects . You will output the output to Format-Table .

 PS C:\> Run-MyScript.ps1 | Format-Table 

They should really be called PowerShell PowerObjectandPipingShell.

+6
Apr 03 '13 at 16:39
source share



All Articles