24 Oct 2011 @ 6:59 AM 

Last weekend I’ve been asked to write a script that will output the size of all files within a folder, and the files in all its sub-folders.
So, again something that’s possible with a one-liner Smile

Get-ChildItem D:\Software -recurse | Select-Object Name, @{Name=”KiloBytes”;Expression={$_.Length / 1KB}}

You don’t want the output to be in Kb but in MB? No problem…

Get-ChildItem D:\Software -recurse | Select-Object Name, @{Name=”MegaBytes”;Expression={$_.Length / 1MB}}

Post to Twitter

Posted By: Jeff Wouters
Last Edit: 23 Apr 2012 @ 06:29 AM

EmailPermalink
Tags
Categories: PowerShell, Script, Windows


  • http://twitter.com/JeffHicks Jeffery Hicks

    You can create script blocks for KB and MB ahead of time
    $mb=@{Name=”MB”;Expression={($_.length/1MB)}}
    $kb=@{Name=”KB”;Expression={($_.length/1kb)}}

    Then use them as you need depending on the folder: dir c:files | select fullname, $mb

    or even format the number

    $mb=@{Name=”MB”;Expression={“{0:F2}” -f ($_.length/1MB)}}

  • http://www.jeffwouters.nl Jeff Wouters

    Script blocks are definitely the way to go when you’re building a script since you can use them multiple times later in the script, but for a one-liner I can’t think of a reason to use it… or am I forgetting something?

  • http://twitter.com/JeffHicks Jeffery Hicks

    I was merely suggesting defining scriptblocks so you could reuse them in other one liners. Suppose you want to check a diffierent folder and you want this to be in MB, just the the MB scriptblock in the select statement. I’m just trying to save some typing.

  • http://www.jeffwouters.nl Jeff Wouters

    Then we are in agreement :-) Your feedback and tips are always welcome btw…

  • http://twitter.com/JeffHicks Jeffery Hicks

    coming full circle: Re-Use #PowerShell Scriptblocks https://plus.google.com/109354722869529171746/posts/RGideinCBTo


Get Adobe Flash player
 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 164
  • Comments » 150
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.

Contact



    No Child Pages.