Finding Nested Object Secrets

by Aug 1, 2016

PowerShell 2+

Objects can have a nested and complex inner structure, and it can be tiresome to examine properties one by one. Here is a simple approach to turn the structure of an object into text:

# find nested object elements up to a given depth
$test = Get-Service -Name spooler
$test | Select-Object * | Format-Custom * -Depth 2

Even a simple service object turns out to be a complex nested structure:

 
class ServiceController
{
  Name = spooler
  RequiredServices = 
    [
      class ServiceController
      {
        Status = Running
        Name = RPCSS
        DisplayName = Remoteprozeduraufruf (RPC)
      }
      class ServiceController
      {
        Status = Running
        Name = http
        DisplayName = HTTP-Dienst
      }
    ]
    
  CanPauseAndContinue = False
  CanShutdown = False
  CanStop = True
  DisplayName = Druckwarteschlange
  DependentServices = 
    [
      class ServiceController
      {
        Status = Stopped
        Name = Fax
        DisplayName = Fax
      }
    ]
    
  MachineName = .
  ServiceName = spooler
  ServicesDependedOn = 
    [
      class ServiceController
      {
        Status = Running
        Name = RPCSS
        DisplayName = Remoteprozeduraufruf (RPC)
      }
      class ServiceController
      {
        Status = Running
        Name = http
        DisplayName = HTTP-Dienst
      }
    ]
    
  ServiceHandle = 
  Status = Running
  ServiceType = Win32OwnProcess, InteractiveProcess
  Site = 
  Container = 
}
 

Twitter This Tip! ReTweet this Tip!