$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
}
… 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.
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? What a waste of time.
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?
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.