Automating the world one-liner at a time…
Following Slashdot's review of Bruce's book (PowerShell in Action ), a ton of people chimed in with comments. One of the most interesting as this one from nacturation:
Well said.
Jeffrey Snover [MSFT]Windows Management Partner ArchitectVisit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShellVisit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
The comments also contained the usual selection of "On *nix I can do X"
[code]
du -x / | sort -nr > mem.txt &
[/code]
was the first I came across and of course I fell into the trap of thinking about how I'd do it in PSH. I came up with:
function global:Measure-Directory
{
param (
[string]$Path = (write-error `
"Usage: Measure-Directory [-Path] path [-Recurse]"),
[switch]$Recurse = $false
)
process {
gci $Path -Rec:$Recurse |
? { $_.psiscontainer } |
% { $dir=$_; gci $_.fullname |
? { -not $_.psiscontainer } |
measure -sum -average -minimum -maximum length |
% {
add-member -name Length -value $_.Sum -membertype NoteProperty -pass -inp $dir |
add-member -name Count -value $_.Count -membertype NoteProperty -pass |
add-member -name Average -value $_.Average -membertype NoteProperty -pass |
add-member -name Minimum -value $_.Minimum -membertype NoteProperty -pass |
add-member -name Maximum -value $_.Maximum -membertype NoteProperty -pass
}
set-alias du Measure-Directory
that would allow me to issue:
[code]du -r / |sort length -desc > mem.txt[/code]
But it would also allow me to issue:
[code]du / -rec |? {$_.count -gt 10}|? {$_.LastWriteTime -gt "2007-01"} |sort length |ft Length,Count,Minimum,Maximum,Average,LastWriteTime,fullname -auto -wrap[/code]