Displaying Array Members in Output

When you output objects that have arrays in their properties, only 4 array elements are displayed, then an ellipsis truncates the rest:

 
PS C:> Get-Process | Select-Object -Property Name, Threads -First 6

Name       Threads                    
----       -------                    
acrotray   {3160}                     
AERTSr64   {1952, 1968, 1972, 8188}   
AGSService {1980, 1988, 1992, 2000...}
armsvc     {1920, 1940, 1944, 7896}   
audiodg    {7436, 1460, 2192, 6784}   
ccSvcHst   {2584, 2644, 2656, 2400...}
 

To show more (or all) array elements, use the internal $FormatEnumerationLimit variable. It defaults to 4, but you can change it to the number of elements you want to display, or set it to -1 to show all elements:

 
PS C:> $FormatEnumerationLimit = 1

PS C:> Get-Process | Select-Object -Property Name, Threads -First 6

Name       Threads  
----       -------  
acrotray   {3160}   
AERTSr64   {1952...}
AGSService {1980...}
armsvc     {1920...}
audiodg    {7436...}
ccSvcHst   {2584...}



PS C:> $FormatEnumerationLimit = 2

PS C:> Get-Process | Select-Object -Property Name, Threads -First 6

Name       Threads        
----       -------        
acrotray   {3160}         
AERTSr64   {1952, 1968...}
AGSService {1980, 1988...}
armsvc     {1920, 1940...}
audiodg    {7436, 2192...}
ccSvcHst   {2584, 2644...}



PS C:> $FormatEnumerationLimit = -1

PS C:> Get-Process | Select-Object -Property Name, Threads -First 6

Name       Threads                                                              
----       -------                                                              
acrotray   {3160}                   
AERTSr64   {1952, 1968, 1972, 8188}                      
AGSService {1980, 1988, 1992, 2000, 2024, 7932}  
armsvc     {1920, 1940, 1944, 7896}
audiodg    {7436, 2192, 6784, 4540, 8040} 
ccSvcHst   {2584, 2644, 2656, 2400, 3080, 3120, 3124, 3128, 3132, 3136, 3140,...
 

When you set it to -1, the list is truncated only when the available space is consumed. To still see all values, explicitly use Format-Table and its –Wrap parameter:

 
PS C:> Get-Process | Select-Object -Property Name, Threads -First 6 | Format-Table -Wrap

Name       Threads                                                              
----       -------                                                              
acrotray   {3160}                                                               
AERTSr64   {1952, 1968, 1972, 8188}                                             
AGSService {1980, 1988, 1992, 2000, 2024, 7932}                                 
armsvc     {1920, 1940, 1944, 7896}                                             
audiodg    {7436, 2192, 6784, 4540, 8040}                                       
ccSvcHst   {2584, 2644, 2656, 2400, 3080, 3120, 3124, 3128, 3132, 3136, 3140,   
           3144, 3232, 3240, 3248, 3260, 3268, 3288, 3304, 3344, 3492, 3552,    
           3556, 3568, 3572, 3576, 3580, 3596, 3600, 3604, 3612, 3616, 3708,    
           3712, 3716, 3724, 3732, 3736, 3760, 3764, 3768, 3776, 3780, 3796,    
           3800, 3804, 3816, 3820, 3824, 3828, 3832, 3844, 3888, 3892, 4232,    
           5084, 5088, 3112, 7100, 7016, 480, 3020, 3044, 4744, 7148, 1828,     
           6476, 6516, 6524, 7160, 6652, 7000, 964, 6028, 4644, 4828, 6664,     
           7892, 5820, 8180, 4940, 5956, 7684, 7156}                            



PS C:> 
 

Twitter This Tip! ReTweet this Tip!

Find All Files Two Levels Deep

Yet another file system task: list all *.log files in a folder structure, but only to a maximum depth of 2 folder structures:

Get-ChildItem -Path c:windows -Filter *.log -Recurse -Depth 2 -File -Force -ErrorAction SilentlyContinue |
  Select-Object -ExpandProperty FullName

Fortunately, PowerShell 5 added the helpful -Depth parameter to Get-ChildItem.

Twitter This Tip! ReTweet this Tip!

Deleting All Subfolders Below A Given Level

Here is another file system task that sounds worse than it actually is. Let’s say you need to remove all folders below a given level in a folder structure. Here is how:

# Task:

# remove all folders one level below the given path
Get-ChildItem -Path "c:sample*" -Directory -Recurse | 
    # remove -WhatIf to actually delete
    # ATTENTION: test thoroughly before doing this!
    # you may want to add -Force to Remove-Item to forcefully delete
    Remove-Item -Recurse -WhatIf

Simply use “*” in your path. To delete all folders two levels below a given path, adjust accordingly:

# Task:

# remove all folders TWO levels below the given path
Get-ChildItem -Path "c:sample**" -Directory -Recurse | 
    # remove -WhatIf to actually delete
    # ATTENTION: test thoroughly before doing this!
    # you may want to add -Force to Remove-Item to forcefully delete
    Remove-Item -Recurse -WhatIf

Twitter This Tip! ReTweet this Tip!