“Braces Secret” with PowerShell.exe

by Sep 11, 2017

Here is a little secret that applies to powershell.exe when it is called from within PowerShell or PowerShell Core: when you run powershell.exe and submit commands via the parameter -Command, PowerShell runs the command and returns plain text:

$a = powershell -noprofile -Command Get-Service
$a[0].GetType().FullName 
System.String

When you place your code in braces, PowerShell will serialize the results and return rich objects:

$a = powershell -noprofile -Command { Get-Service }
$a[0].GetType().FullName 
System.Management.Automation.PSObject 

$a[0] | Select-Object -Property * 

Name                : AdobeARMservice
RequiredServices    : {}
CanPauseAndContinue : False
CanShutdown         : False
CanStop             : True
DisplayName         : Adobe Acrobat Update Service
DependentServices   : {}
MachineName         : .
ServiceName         : AdobeARMservice
ServicesDependedOn  : {}
Status              : Running
ServiceType         : Win32OwnProcess
StartType           : Automatic
Site                : 
Container           :

Twitter This Tip! ReTweet this Tip!