Help with script output

I downloaded the Get-DirStats.ps1 script from technet and it is working great except for one small aspect. The output is truncated and makes it useless for me. Here is an example of the output I am getting.

Path Files Size


C:\users 1 174

C:\users\All Users 1340 428744744

C:\users\Default 61 2681636

C:\users\Defaul… 0 0

C:\users\defaul… 0 0

C:\users\Public 12 6659

C:\users\User 12426 …909267

 

I have looked through the code for about an hour but being a novice I am not seeing where it is limiting those tables. I don’t care how long they are, I want the full output. Here is the code.

Get-DirStats.ps1

Written by Bill Stewart (bstewart@iname.com)

Outputs file system directory statistics.

 

#requires -version 2

 

 

[CmdletBinding(DefaultParameterSetName=“Path”)]

param(

[parameter(Position=0,Mandatory=$false,ParameterSetName=“Path”,ValueFromPipeline=$true)]

$Path=(get-location).Path,

[parameter(Position=0,Mandatory=$true,ParameterSetName=“LiteralPath”)]

[String[]] $LiteralPath,

[Switch] $Only,

[Switch] $Every,

[Switch] $FormatNumbers,

[Switch] $Total

)

 

begin {

$ParamSetName = $PSCmdlet.ParameterSetName

if ( $ParamSetName -eq “Path” ) {

$PipelineInput = ( -not $PSBoundParameters.ContainsKey(“Path”) ) -and ( -not $Path )

}

elseif ( $ParamSetName -eq “LiteralPath” ) {

$PipelineInput = $false

}

 

Script-level variables used with -Total.

[UInt64] $script:totalcount = 0

[UInt64] $script:totalbytes = 0

 

Returns a [System.IO.DirectoryInfo] object if it exists.

function Get-Directory {

param( $item )

 

if ( $ParamSetName -eq “Path” ) {

if ( Test-Path -Path $item -PathType Container ) {

$item = Get-Item -Path $item -Force

}

}

elseif ( $ParamSetName -eq “LiteralPath” ) {

if ( Test-Path -LiteralPath $item -PathType Container ) {

$item = Get-Item -LiteralPath $item -Force

}

}

if ( $item -and ($item -is [System.IO.DirectoryInfo]) ) {

return $item

}

}

 

Filter that outputs the custom object with formatted numbers.

function Format-Output {

process {

$_ | Select-Object Path,

@{Name=“Files”; Expression={"{0:N0}" -f $_.Files}},

@{Name=“Size”; Expression={"{0:N0}" -f $_.Size}}

}

}

 

Outputs directory statistics for the specified directory. With -recurse,

the function includes files in all subdirectories of the specified

directory. With -format, numbers in the output objects are formatted with

the Format-Output filter.

function Get-DirectoryStats {

param( $directory, $recurse, $format )

 

Write-Progress -Activity “Get-DirStats.ps1” -Status “Reading ‘$($directory.FullName)’”

$files = $directory | Get-ChildItem -Force -Recurse:$recurse | Where-Object { -not $_.PSIsContainer }

if ( $files ) {

Write-Progress -Activity “Get-DirStats.ps1” -Status “Calculating ‘$($directory.FullName)’”

$output = $files | Measure-Object -Sum -Property Length | Select-Object `

@{Name=“Path”; Expression={$directory.FullName}},

@{Name=“Files”; Expression={$.Count; $script:totalcount += $.Count}},

@{Name=“Size”; Expression={$.Sum; $script:totalbytes += $.Sum}}

}

else {

$output = “” | Select-Object `

@{Name=“Path”; Expression={$directory.FullName}},

@{Name=“Files”; Expression={0}},

@{Name=“Size”; Expression={0}}

}

if ( -not $format ) { $output } else { $output | Format-Output }

}

}

 

process {

Get the item to process, no matter whether the input comes from the

pipeline or not.

if ( $PipelineInput ) {

$item = $_

}

else {

if ( $ParamSetName -eq “Path” ) {

$item = $Path

}

elseif ( $ParamSetName -eq “LiteralPath” ) {

$item = $LiteralPath

}

}

 

Write an error if the item is not a directory in the file system.

$directory = Get-Directory -item $item

if ( -not $directory ) {

Write-Error -Message “Path ‘$item’ is not a directory in the file system.” -Category InvalidType

return

}

 

Get the statistics for the first-level directory.

Get-DirectoryStats -directory $directory -recurse:$false -format:$FormatNumbers

-Only means no further processing past the first-level directory.

if ( $Only ) { return }

 

Get the subdirectories of the first-level directory and get the statistics

for each of them.

$directory | Get-ChildItem -Force -Recurse:$Every |

Where-Object { $_.PSIsContainer } | ForEach-Object {

Get-DirectoryStats -directory $_ -recurse:(-not $Every) -format:$FormatNumbers

}

}

 

end {

If -Total specified, output summary object.

if ( $Total ) {

$output = “” | Select-Object `

@{Name=“Path”; Expression={""}},

@{Name=“Files”; Expression={$script:totalcount}},

@{Name=“Size”; Expression={$script:totalbytes}}

if ( -not $FormatNumbers ) { $output } else { $output | Format-Output }

}

}

 

Just use the Format-Table cmdlet with the Autosize switch. Basic stuff really. You say you are new to this. Best advice is to take advantage of all the no cost PS training via MS Virtual Academy and the hundreds of videos on YouTube.

Take advantage of the no cost to use TechNet virtual labs and all the no cost PS eBooks on this site and on MS sites.

Don’t stress yourself out unnecessarily. Take the training, Read the help files, then read them again. It’s all in there. Really, it is.

.\Get-DirStats.ps1

Path                Files      Size
----                -----      ----
D:\Scripts           6232  53322842
D:\Scripts\CheckURI     6    637910
D:\Scripts\GUI-WPF    212   4730071
D:\Scripts\UsbTr...    14     97840
D:\Scripts\VSCode      23    386634



.\Get-DirStats.ps1 | Format-Table -AutoSize

Path                          Files      Size
----                          -----      ----
D:\Scripts                     6232  53322842
D:\Scripts\CheckURI               6    637910
D:\Scripts\GUI-WPF              212   4730071
D:\Scripts\UsbTreeView-master    14     97840
D:\Scripts\VSCode                23    386634

The formatting system in particular can be tricky, and you’re just missing some of the “secret under the hood sauce” that makes PowerShell work, is all. Like, the “output” on your screen isn’t actually the “output” per se - it’s the output, plus a bunch of formatting voodoo. “Learn Windows PowerShell in a Month of Lunches” is a good resource as well, if you’re looking for something a little more structured than roaming around YouTube, but the original companion videos are on my youtube.com/powershelldon channel.

I knew it was going to be something super easy that I was overlooking. I was going through that script line by line trying to find the formatting there. I never even thought about passing it with the command. Thanks for the quick help, now I get to let this run for a couple of days while I query user directory sizes on 10k servers.

I think the idea is it doesn’t know how wide the column has to be from the first line. Often scripts put the widest column farthest to the right. But this seems to work better in ps 5.1 than ps 5.0. Other ways to make it look better are to save the output to a variable, pipe it to more, or pipe it to sort. In all cases you have to wait until the whole thing is done before viewing it.

btwn,I request you to use formating tags while posting the code in the forum which makes other to easily understand your code, below link will help you.

kvprasoon, the code kevin posted is the exact code from the TechNet site where he downloaded it from https://gallery.technet.microsoft.com/scriptcenter/Outputs-directory-size-964d07ff, but still, yeppers on the formatting, even if copying and pasting from elsewhere. ;^}