How to run the powershell code for all system drives? -


at moment code runs against drive c: run/apply code below existing system drives sub-folders contents, i.e. c:\ d:\ e:\ , etc. suggestion on how achieve this? if need me clarify question, ask. (**i want $dir substitute system drive roots).

$dir = "c:\" #this main client-side file scanning code use on client computers $count = @{} $size = @{} $hostname = @{} gci $dir -recurse |%{ [int]$count[$_.extension] += 1 [int64]$size[$_.extension] += $_.length } $results = @() $count.keys | sort |% { $result = ""|select extension,count,size,hostname $result.extension = $_ $result.count = $count[$_] $result.size = [math]::round($size[$_] /1gb, 3) $result.hostname = $(get-content env:computername) $results += $result } $results | ft -auto  $dirname = "c:\inetpub\wwwroot\${env:computername}" if (!(test-path $dirname)) { mkdir $dirname }  $results | sort-object -property size -descending | select-object -first 30| export-csv c:\"$env:computername-$(get-date -f dd-mm-yyyy-hh-mm)".csv  $a = "<style>" $a = $a + "body{background-color:#a987cc;}" $a = $a + "table{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;.center { margin:auto; width:70%; };}" $a = $a + "th{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#99ccff}" $a = $a + "td{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:palegoldenrod}" $a = $a + "</style>"   $results | sort-object -property size -descending | select-object -first 30 | convertto-html extension,count,size, hostname "$a" -title "just" -body "top 30 files" |  set-content c:\inetpub\wwwroot\${env:computername}\"$env:computername-$(get-date -f dd-mm-yyyy-hh-mm)".htm 

you pretty have it, needed listing of physical drives on computer , loop through it.

a couple other notes:

  • i removed $results |ft -auto. had in there debug output.
  • you twice: $results | sort-object -property size -descending | select-object -first 30. did once, storing output $results. multiple trips of same data through same pipeline steps inefficient.
  • i made $a here-string. easier read & work concatenating strings.
  • i reformatted date string in output files can sort filenames date.
  • don't put files in c:\. it's sloppy. find more appropriate location.
  • this capture folders (you aren't filtering them out). desired?

    $drives = get-wmiobject win32_logicaldisk -filter "drivetype=3"|select -expandproperty deviceid;  #this main client-side file scanning code use on client computers $results = @() foreach ($drive in $drives){ $count = @{} $size = @{} $hostname = @{} gci $drive -recurse |%{     [int]$count[$_.extension] += 1     [int64]$size[$_.extension] += $_.length }    $count.keys | sort |% {     $result = ""|select extension,count,size,hostname;     $result.extension = $_;     $result.count = $count[$_];     $result.size = [math]::round($size[$_] /1gb, 3);     $result.hostname = $(get-content env:computername);     $results += $result; } }  $dirname = "c:\inetpub\wwwroot\${env:computername}" if (!(test-path $dirname)) { new-item -itemtype directory $dirname } $results = $results|sort-object -property size -descending|select-object -first 30;  $results | export-csv c:\"$env:computername-$(get-date -f yyyy-mm-dd-hh-mm)".csv  $a = @" <style> body{background-color:#a987cc;} table{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;.center { margin:auto; width:70%; };} th{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:#99ccff} td{border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:palegoldenrod} </style> "@  $results | convertto-html extension,count,size, hostname "$a" -title "just" -body "top 30 files" | set-content c:\inetpub\wwwroot\${env:computername}\"$env:computername-$(get-date -f yyyy-mm-dd-hh-mm)".htm 

Comments