Get-ChildItem | Measure-Object not giving me output

Hello,
I have this script:

$Computername = Read-Host -Prompt "Please enter computer"
$path = Read-Host -Prompt "Please enter path"


Invoke-Command -ComputerName $Computername {

    # Initialize an empty array to hold the output objects
    $Output = @()
 
    # Get all folders within the top folder
    $Folders = Get-ChildItem $Using:Path

    # Loop through each folder
    foreach ($Folder in $Folders) {
        # Get the number of files in the folder
        $NumFolders = Get-ChildItem $Folder.FullName -Recurse -Force | Where-Object { $_.PSIsContainer } | Measure-Object | Select-Object count
      
        $numfiles = Get-ChildItem $Folder.FullName -Recurse -Force -File | Measure-Object | Select-Object count

        # Get the total size of the folder
        $TotalSize = Get-ChildItem -Recurse -Verbose | Measure-Object -Property Length -Sum

        # Get the size of the largest file in the folder
        $LargestFile = Get-ChildItem $Folder.FullName -File | Measure-Object -Property Length -Sum | Sort-Object -Property Length -Descending | Select-Object -First 1
        $LargestFileSize = ($LargestFile.Value / 1MB)


        # Create a custom object with the output values
        $OutputObject = [PSCustomObject]@{
            "FolderName"        = $Folder.fullName
            "NumFolders"        = $NumFolders
            "NumFiles"          = $numfiles
            "TotalSizeMB"       = "{0:N2}" -f ("$TotalSize" / 1MB)
            "LargestFileSizeMB" = "{0:N2}" -f $LargestFileSize
        }

        # Add the output object to the output array
        $Output += $OutputObject
    }

    # Sort the output by the "NumFiles" column from largest to smallest
    $Output | Sort-Object -Property FolderName | Format-Table
}

The output looks like this:

FolderName                                            NumFolders    NumFiles   TotalSizeMB LargestFileSizeMB
----------                                            ----------    --------   -----------  -----------------
X:\Shares\DevProfiles\user1.mydomain.v6          @{Count=8}    @{Count=1}			0.00			0.00
X:\Shares\DevProfiles\user2.mydomain.v6        	 @{Count=376}  @{Count=518}			0.00			0.00
X:\Shares\DevProfiles\user3.mydomain.v6          @{Count=43}   @{Count=52}			0.00			0.00
X:\Shares\DevProfiles\user4.mydomain.v6          @{Count=212}  @{Count=324}			0.00			0.00
X:\Shares\DevProfiles\user5.mydomain.v6 		 @{Count=79}   @{Count=63}			0.00			0.00
X:\Shares\DevProfiles\user6.mydomain.v6 		 @{Count=47}   @{Count=56}			0.00			0.00
X:\Shares\DevProfiles\user7.mydomain.v6 		 @{Count=47}   @{Count=56}			0.00			0.00

Can anyone help me remove the @{count=x} and just have the number, thats a minor thing but would be nice.

More important, anyone know why I get 0 in the 2 size colomns?

Thanks!

Your code is a little inefficient and a little overcommented. :wink:

You are querying the same folder 4 times. Instead you should save the result once and work with the saved result. … like this:

$Computername = Read-Host -Prompt 'Please enter computer'
$Path = Read-Host -Prompt 'Please enter path'

$Result = 
Invoke-Command -ComputerName $Computername -ScriptBlock {

    $FolderList = Get-ChildItem $Using:Path -Directory

    foreach ($Folder in $FolderList) {
        $FileSystemObjectList = 
            Get-ChildItem $Folder.FullName -Recurse -Force
        $FolderList = 
            $FileSystemObjectList | 
                Where-Object -Property PSIsContainer -EQ -Value $true
        $FileList = 
            $FileSystemObjectList | 
                Where-Object -Property PSIsContainer -EQ -Value $false
        $TotalSize = 
            $FileList | 
                Measure-Object -Property Length -Sum -Maximum
        [PSCustomObject]@{
            FolderName        = $Folder.FullName
            NumFolders        = $FolderList.Count
            NumFiles          = $FileList.Count
            TotalSizeMB       = [MATH]::Round($TotalSize.Sum / 1MB, 2)
            LargestFileSizeMB = [MATH]::Round($TotalSize.Maximum / 1MB, 2)
        }
    }
}

$Result | 
    Sort-Object -Property PSComputerName, FolderName |
        Select-Object -Property * -ExcludeProperty RunspaceId |
            Format-Table -AutoSize

… and it is actually better to get the results as raw as possible and transform, sort, select and format them only later at the moment you need them the way you need them. This way you do not limit yourself to the one particular format.
As you can see I kept the property PSComputerName. You can run this code actually against an array of computers and so you can sort the output by computername first and second by folder name.

Thank You Olaf, yea I see that’s much better.

I made some tweaks. One of the paths i was looking at has 9,000+ profiles dirs in it.

This shows a progress bar and each record its on as its processed:

$Computername = Read-Host -Prompt 'Please enter computer'
$Path = Read-Host -Prompt 'Please enter path'

$Result = Invoke-Command -ComputerName $Computername -ScriptBlock {
    $FolderList = Get-ChildItem $Using:Path -Directory
    $TotalFolders = $FolderList.Count
    $Progress = 0
    
    foreach ($Folder in $FolderList) {
        Write-Progress -Activity "Processing folder $($Folder.FullName)" -PercentComplete (($Progress / $TotalFolders) * 100) -Status "Processed $($Progress) out of $($TotalFolders) folders"
        $FileSystemObjectList = Get-ChildItem $Folder.FullName -Recurse -Force
        $FolderList = $FileSystemObjectList | Where-Object -Property PSIsContainer -EQ -Value $true
        $FileList = $FileSystemObjectList | Where-Object -Property PSIsContainer -EQ -Value $false
        $TotalSize = $FileList | Measure-Object -Property Length -Sum -Maximum
        [PSCustomObject]@{
            FolderName        = $Folder.FullName
            NumFolders        = $FolderList.Count
            NumFiles          = $FileList.Count
            TotalSizeMB       = [MATH]::Round($TotalSize.Sum / 1MB, 2)
            LargestFileSizeMB = [MATH]::Round($TotalSize.Maximum / 1MB, 2)
        }
        $Progress++
    }
}
$Result | Select-Object -Property * -ExcludeProperty RunspaceId, PSShowComputerName | 
Export-Excel -Path c:\temp\$Computername-UPMProfiles.xlsx -Autosize -AutoFilter -FreezeTopRow

That usually slows down the code a lot. If you’re looking for speed you should avoid any console output. I’d recommend to make it optional with a switch.

Do you really sit there and watch the progress bar growing? :smirk: What a waste of time. :man_shrugging:t4:

Now really, do you think I sit there and watch it? I don’t think so.

The script can run for over 2 hours with or without a progress bar. With a progress bar, I can see how far along it is. Is it that bad of an idea? Why have code to produce a progress bar, if your not going to use it?

Why always the little digs?

I would take the comments on the progress bar as food for thought. For a small data set like you describe, not sure you gain anything from the progress bar. For a larger data set, the loss of speed might be outweighed by the information. Only you can decide what is right for your script/environment.

That’s always the case. Tips or suggestions you get here or in other forums are just this - tips or suggestions - not lawful orders. Of course you can do whatever you like or need in your environment.

If the progress bar serves a particular purpose you’re ken to have you should certainly keep it.

Some folks like to have a bright and colorful console while other prefer to keep it simple black and white. And none of it is wrong - it’s just different. :man_shrugging: :+1:t4: