Link to home
Start Free TrialLog in
Avatar of techdrive
techdriveFlag for United States of America

asked on

poweshell script problem

I am trying to get the MB OR KB or GB info to print out but for some reason this is not working. I am trying to get the size and names of folders on our network. ANy help would be appreciative, thanks.


ForEach-Object { dir \\servername\share | Where-Object { $_.PSisContainer } } |
ForEach-Object { Write-Progress 'file is processing' ($_.FullName); $_ } |
ForEach-Object { $result = '' | Select-Object Path, Count, Size; $result.path = $_.FullName;
$temp = Dir $_.FullName -recurse -ea SilentlyContinue | Measure-Object length -sum -ea SilentlyContinue ;
Select-Object @{n='Name';e={ $_ }},@{n='Size';e={
                        If      (($result.Size / 1Gb) -ge 1) { "$('{0:D2}' -f ($result.Size / 1Gb)) Gb" }
                        ElseIf (($result.Size / 1Mb) -ge 1) { "$('{0:N2}' -f ($result.Size / 1Mb)) Mb" }
                        ElseIf (($result.Size / 1kb) -ge 1) { "$('{0:N2}' -f ($result.Size / 1Kb)) Kb" }
                        Else   { "$($result.Size) b" } }}} | | Export-Csv "reportt.csv" -NoTypeInformation
Avatar of soostibi
soostibi
Flag of Hungary image

think you would like to see something like this. In your script pipelines are completely mixed up.
dir \\servername\share | Where-Object { $_.PSisContainer } | 
ForEach-Object { 
    Write-Progress 'file is processing' ($_.FullName)
    $Size = Dir $_.FullName -recurse -ea SilentlyContinue | Measure-Object length -sum -ea SilentlyContinue 
    New-Object -TypeName PSObject -Property @{
        Path = $_.fullname
        Size = $size.sum
        Count = $size.count
    }
} | Select-Object @{n='Name';e={ $_.path }},
        @{n='Size';e={
                        If      (($_.Size / 1Gb) -ge 1) { "$('{0:N2}' -f ($_.Size / 1Gb)) Gb" }
                        ElseIf (($_.Size / 1Mb) -ge 1) { "$('{0:N2}' -f ($_.Size / 1Mb)) Mb" }
                        ElseIf (($_.Size / 1kb) -ge 1) { "$('{0:N2}' -f ($_.Size / 1Kb)) Kb" }
                        Else   { "$($_.Size) b" } }
                    } | 
Export-Csv "reportt.csv" -NoTypeInformation

Open in new window

Avatar of Chris Dent

That's a lot of loops... The first is not necessary, so lets drop that one right away. You only actually need one.

You make $result, and give no value for size, which is why it can't show any.
dir \\servername\share | Where-Object { $_.PSisContainer } | ForEach-Object {

  Write-Progress "File is processing" -Status  $_.FullName

  # This is more efficient than Measure-Object (uses less RAM / should be faster)
  $Size = 0; $Count = 0
  dir $_.FullName -Recurse -ErrorAction SilentlyContinue | ForEach-Object { $Size += $_.Length; $Count++ }

  $_ | Select-Object Name, @{n='Count';e={ $Count }}, @{n='Size';e={
    If     ($Size -gt 1Gb) { "$('{0:N2}' -f ($Size / 1Gb)) Gb" }
    ElseIf ($Size -gt 1Mb) { "$('{0:N2}' -f ($Size / 1Mb)) Mb" }
    ElseIf ($Size -gt 1Kb) { "$('{0:N2}' -f ($Size / 1Kb)) Kb" }
    Else                   { "$Size b" } }}

} | Export-Csv "report.csv" -NoTypeInformation

Open in new window

HTH

Chris
Avatar of techdrive

ASKER

Chris,


You are truly sir the master of powershell. Is it any book or resource you can point me to in order for me to advance my knowledge.
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial