Sunday, October 2, 2022
HomePowershellMethods to Use $FormatEnumerationLimit - PowerShell Neighborhood

Methods to Use $FormatEnumerationLimit – PowerShell Neighborhood


Q: Once I format an object the place a property comprises greater than 4 objects, I by no means see the additional property values. How can I repair that?

A: Use the $FormatEnumerationLimit variable.

This question is one I hear in lots of PowerShell assist boards, and I’ve encountered this problem quite a bit over time. What occurs is that you just problem a command to return objects, for instance Get-Course of. The Get-* cmdlets return objects which may comprise properties which can be arrays of values, not only a single worth. While you pipe these objects to Format-Desk, by default, PowerShell solely reveals you the primary 4.

Let me illustrate what this appears to be like like (by default):

PS> Get-Course of -Identify pwsh | Format-Desk -Property ProcessName, Modules  

ProcessName Modules  
----------- -------  
pwsh        {System.Diagnostics.ProcessModule (pwsh.exe), 
             System.Diagnostics.ProcessModule (ntdll.dll),
             System.Diagnostics.ProcessModule (KERNEL32.DLL), 
             System.Diagnostics.ProcessModule (KERNELBASE.dll)…}

This output reveals PowerShell getting the method object for Pwsh.exe after which passing it to Format-Desk, which outputs the method identify and the modules utilized by that course of. Nevertheless, as you may see, PowerShell reveals solely 4 modules proven adopted by “…” (often known as an ellipsis). The ellipsis tells you that there are extra values on this property, besides PowerShell doesn’t present them.

If you realize the Format-Desk command, you may be tempted to make use of the -Wrap or the -AutoSize parameters, however these wouldn’t assist. It seems there isn’t a parameter for Format-Desk or Format-Record to regulate this. The trick is to make use of the $FormatEnumerationLimit variable and assign it a better worth.

The $FormatEnumerationLimit desire variable tells PowerShell and the formatting cmdlets what number of occurrences to incorporate within the formatted output. By default, PowerShell units this variable to 4 at startup. And that’s the reason you see simply 4 processes in output (by default).

With PowerShell, you may modify this restrict in a script or a profile file. While you change the worth, PowerShell outputs extra occurrences, as much as the restrict you set in $FormatEnumerationLimit. Like this:

PS > $FormatEnumerationLimit = 8
PS > Get-Course of -Identify PWSH | Format-Desk -Property ProcessName, Modules

ProcessName Modules
----------- -------
pwsh        {System.Diagnostics.ProcessModule (pwsh.exe), 
             System.Diagnostics.ProcessModule (ntdll.dll),
             System.Diagnostics.ProcessModule (KERNEL32.DLL),
             System.Diagnostics.ProcessModule (KERNELBASE.dll),
             System.Diagnostics.ProcessModule (apphelp.dll),
             System.Diagnostics.ProcessModule (USER32.dll),
             System.Diagnostics.ProcessModule (win32u.dll),
             System.Diagnostics.ProcessModule (GDI32.dll)…}   

Within the above output, you may see output for eight modules. In penning this, there are literally 239 precise modules for the PowerShell course of. If you have to see all of the modules, you can set $FormatEnumerationLimit to a bigger quantity (e.g. 999) within the shell. Alternatively, in case you set $FormatEnumerationLimit to -1, PowerShell shows all occurrences, which can be greater than you need most often! I set the restrict to 99 in my profile file and that’s normally greater than enough.

Scoping of $FormatEnumerationLimit

One fascinating factor I discovered is that $FormatEnumerationLimit is scoped in another way to my expectations. In case you use a format command inside a perform or script (a toddler of the worldwide scope), the command solely makes use of the worth from the worldwide scope.

The next code comprises a perform for instance the problem:

perform Take a look at-FormatLimitLocal
 Choose-Object -Property Identify, Threads -First 4

You may suppose that this code would show the primary thread in every of the primary 4 processes. You may, however you’d be incorrect, as you may see right here:

PS> # Right here present the worth and name the functin
PS> "Earlier than calling: [$FormatEnumerationLimit]"
Earlier than calling: [4]
PS> Take a look at-FormatLimitLocal
In Operate, restrict is: [4]
After altering: [1]

Identify                    Threads
----                    -------
AggregatorHost          {5240}
ApplicationFrameHost    {16968, 2848}
AppVShNotify            {9164}
Atom.SDK.WindowsService {4064, 4908, 4912, 19144…}

As you may see from this output, the ultimate course of reveals FOUR threads not ONE. It’s because PowerShell appears to solely use the globally scoped worth, not the domestically scoped copy. To get round this curious scoping, you may re-write the perform like this:


perform Take a look at-FormatLimitGlobal
 Choose-Object -Property Identify, Threads -First 4
  # Change it again
  $International:FormatEnumerationLimit = $Previous

While you name the up to date perform, it now operates extra as you may want, like this:

PS> # View the worth
PS> "Earlier than calling: [$FormatEnumerationLimit]"
Earlier than calling: [4]#
PS> # Now name the up to date perform
PS> Take a look at-FormatLimitGlobal
After altering: [1]

Identify                    Threads
----                    -------
AggregatorHost          {5240}
ApplicationFrameHost    {16968…}
AppVShNotify            {9164}
Atom.SDK.WindowsService {4064…}

So, with some cautious updating of the worldwide variable, you will get the specified consequence. Typically, I train my college students to keep away from manipulating international variables from inside a script or a perform (except you realize what you’re doing). If you have to make modifications to any international variable to make a perform or script do what you need, guarantee you understand how to revert the variable to its unique worth.

I’m unclear whether or not this can be a bug or a characteristic! To that finish, I submitted a characteristic request within the PowerShell supply repository. Be at liberty so as to add your opinion within the feedback or upvote it if you wish to see it added.

Abstract

The $FormatEnumerationLimit variable is a neat characteristic of PowerShell that means that you can see extra occurrences when utilizing Format-Desk. However keep in mind: in case you are utilizing this variable in a perform or a script, try to be conscious of the scoping problem.

You’ll be able to learn extra about $FormatEnumerationLimit, and different desire variables in about_Preference_Variables.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments