Monthly Archive

Categories

Avoid the truncation in the display

A question came up regarding my recent post about using the file system COM object to get folder sizes. The question was about the folder path and how you could see the whole path. In other words how to avoid the truncation in the display and the three dots.

When PowerShell displays objects it will automatically use a table format if the object has four properties or less. It uses a list if there are more properties. This behaviour can be overridden by PowerShell’s formatting system. This behaviour can lead to truncation of one or more fields.

This is the function to get folder sizes

function Get-FolderSize { 
 [CmdletBinding()] 
param ( 
  [string]$path = 'C:\MyData' 
 )

if (-not (Test-Path -Path $path)){ 
  Throw "$path - Path not found" 
 }

$fso = New-Object -ComObject "Scripting.FileSystemObject"

Get-ChildItem -Path $path -Directory -Recurse | 
foreach { 
  
  $size = ($fso.GetFolder("$($PSItem.FullName)")).Size 
  
  $props = [ordered]@{ 
    Name = $PSItem.Name 
    Created = $PSItem.CreationTime 
    FilePath = $PSItem.FullName 
    SizeMB = [math]::Round( ($size / 1mb), 2) 
  }

  New-Object -TypeName PSObject -Property $props 
 }

}

 

If you just use the function as is

PS> Get-FolderSize -path C:\MyData\OneDrive\Data\Scripts

You can get truncated output – and you don’t see the size!

Office-Excel                         10/02/2017 19:16:29 C:\MyData\OneDrive\Data\Scripts\Office-... 
Office-OneNote                       10/02/2017 19:16:26 C:\MyData\OneDrive\Data\Scripts\Office-... 
Office-Outlook                       10/02/2017 19:16:29 C:\MyData\OneDrive\Data\Scripts\Office-... 
Office-PowerPoint                    10/02/2017 19:16:29 C:\MyData\OneDrive\Data\Scripts\Office-... 
Office-Visio                         10/02/2017 19:18:00 C:\MyData\OneDrive\Data\Scripts\Office-... 
Office-Word                          10/02/2017 19:16:23 C:\MyData\OneDrive\Data\Scripts\Office-...

 

You could use Format-List

PS> Get-FolderSize -path C:\MyData\OneDrive\Data\Scripts | Format-List

which means you get lots of displays like this:

Name     : Event Filters 
Created  : 10/02/2017 19:16:31 
FilePath : C:\MyData\OneDrive\Data\Scripts\WMICookBook\PowerEvents\PowerEvents\Samples\Event 
           Filters 
SizeMB   : 0.02

 

Using Format-Table with the –wrap parameter will avoid the truncation

PS> Get-FolderSize -path C:\MyData\OneDrive\Data\Scripts | Format-Table –AutoSize –Wrap

Samples                                10/02/2017 19:16:31 C:\MyData\OneDrive\Data\Scripts\WMICookBook\PowerEvent 
                                                            s\PowerEvents\Samples                                 
Event Consumers                        10/02/2017 19:16:31 C:\MyData\OneDrive\Data\Scripts\WMICookBook\PowerEvent 
                                                           s\PowerEvents\Samples\Event Consumers                 
Event Filters                          10/02/2017 19:16:31 C:\MyData\OneDrive\Data\Scripts\WMICookBook\PowerEvent 
                                                            s\PowerEvents\Samples\Event Filters                   
ConfigMgr                              10/02/2017 19:16:31 C:\MyData\OneDrive\Data\Scripts\WMICookBook\PowerEvent 
                                                            s\PowerEvents\Samples\Event Consumers\ConfigMgr

 

Notice that the Size is again cut off.  You need to ensure that your output pane in ISE or the PowerShell console is wide enough to display all the fields or PowerShell will truncate the display – without telling you

Comments are closed.