Skip to the main content.

Unlocking the Power of PowerShell: Tips for Success

Discover PowerShell’s hidden formatting features

Discover PowerShell's hidden configuration variables! In tip #13, we delve into $ofs and $FormatEnumerationLimit –two under-the-radar settings that impact array formatting and output display. Learn how to use them to enhance readability and prevent cutoffs in console displays. Unlock the full potential of your PowerShell outputs!

Find out more about hidden configuration variables

There are two important configuration variables in PowerShell that have great impact on formatting, yet aren’t always seen.

$ofs (output field separator) contains one space by default. It is the character(s) that PowerShell uses when automatically turning arrays into strings:


PS C:\> $lottery = 1..49 | Get-Random -Count 7

PS C:\> "Lottery Numbers: $lottery"
Lottery Numbers: 22 2 48 44 23 13 4

PS C:\> $ofs = ', '

PS C:\> "Lottery Numbers: $lottery"
Lottery Numbers: 22, 2, 48, 44, 23, 13, 4

PS C:\> $ofs = ','

PS C:\> "Lottery Numbers: $lottery"
Lottery Numbers: 22,2,48,44,23,13,4

 

$FormatEnumerationLimit defaults to 4. This is the number of array elements that PowerShell includes in any string conversion preview. Here is an example:


Get-Service | 
   Where-Object { $_.DependentServices.Count -gt 1 } |
   Select-Object -Property Name, Dep* 

 

You get a list with services that have at least 2 dependent services. When you look closely at the results, you see that a maximum of 4 dependent services are visible. If a service has more, the list is cut off, and "…" is shown:


NlaSvc               {NcdAutoSetup, AppVClient, netprofm}                       
nsi                  {WlanSvc, icssvc, Wcmsvc, upnphost...}                     
p2pimsvc             {PNRPAutoReg, p2psvc, PNRPsvc}                             
PNRPsvc              {PNRPAutoReg, p2psvc}                                      
ProfSvc              {XblGameSave, TokenBroker, UserManager, shpamsvc...}       
SamSs                {MSDTC, KAPSService, LanmanServer, KtmRm}   


When you set $FormatEnumerationLimit to any other number, you can control this limit. Setting it to -1 disables any limit and always shows all elements:

 

  • PS C:\> $FormatEnumerationLimit = -1 
    PS C:\> $FormatEnumerationLimit = -1 


Now all entries are visible:


NlaSvc               {NcdAutoSetup, AppVClient, netprofm}                                                           
nsi                  {WlanSvc, icssvc, Wcmsvc, upnphost, SSDPSRV, NcdAutoSetup, AppVClient, netprofm, NlaSvc, KND...
p2pimsvc             {PNRPAutoReg, p2psvc, PNRPsvc}                                                                 
PNRPsvc              {PNRPAutoReg, p2psvc}                                                                          
ProfSvc              {XblGameSave, TokenBroker, UserManager, shpamsvc, NaturalAuthentication, Appinfo}              
SamSs                {MSDTC, KAPSService, LanmanServer, KtmRm}   


Except, with "nsi" in this example, there is still a "…" and cut-off. This time, however, it was triggered by the console space constraints. To overcome these as well, you’d need to add Format-Table:


Get-Service | 
   Where-Object { $_.DependentServices.Count -gt 1 } |
   Select-Object -Property Name, Dep* |
   Format-Table -Wrap -AutoSize 


Now the output finally lists all output without cutting off anything:


NlaSvc               {NcdAutoSetup, AppVClient, netprofm}                                                           
nsi                  {WlanSvc, icssvc, Wcmsvc, upnphost, SSDPSRV, NcdAutoSetup, AppVClient, netprofm, NlaSvc,       
                     KNDBWM, KAPSService, Netman, NcaSvc, SessionEnv, Netlogon, LanmanWorkstation, Killer Network   
                     Service, jhi_service, IpxlatCfgSvc, iphlpsvc, XboxNetApiSvc, IKEEXT, hns, Dnscache,            
                     WinHttpAutoProxySvc, Dhcp}                                                                     
p2pimsvc             {PNRPAutoReg, p2psvc, PNRPsvc}                                                                 
PNRPsvc              {PNRPAutoReg, p2psvc}                                                                          
ProfSvc              {XblGameSave, TokenBroker, UserManager, shpamsvc, NaturalAuthentication, Appinfo}              
SamSs                {MSDTC, KAPSService, LanmanServer, KtmRm}   

 

Good2know

Your ultimate PowerShell Cheat Sheet

Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.

The poster is available for download and in paper form.

PowerShell Poster 2023

Get your poster here!

 

 

Related links 

 

Related posts

4 min read

For more clarity: Convert PowerShell error records into actionable data

Tired of juggling PowerShell error handling? Discover how to simplify your workflow with a versatile function that...

3 min read

What's catch without try in PowerShell? It's a trap – Read how to use it!

Are you familiar with try...catch error handling? What is a catch block without a try block? It's a trap – and here is...

3 min read

Seamless PowerShell: Handle errors without missing a beat!

As $error is a global variable, consider using your own logging variable for error handling so you don't affect other...

About the author: