1

On Linux, I can generate a CSV output with the following command

du -hs /home/*/imap/*/* | sort -rh | sed 's/\//\t/g' | awk '{print $5 " " $6 " " $1}' | sed 's/ /,/g'

This command outputs a CSV file with the following structuur:

 domain,username,userfoldersize 

I am trying to get a similar output with PowerShell.

I found this code online, but it does not display sizes correct. Foldersizes over GB sizes, are still displayed in MB. Also, it goes to far in the subdirectory.

$startFolder = 'C:\path\to\mail\domain\user'

$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N2}" -f ($colItems.sum / 1MB) + " MB"

$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
    {
        $subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
        $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
    }

I have a directory like this: C:\path\to\mail\domain\user and I want it to display the full path to the user folder, and display the user foldersize. In a human readable format. e.g.: 500MB, 5GB etc...

  • what report did you try that's failing? – SpiderIce Oct 23 '17 at 15:05
  • Try gci -Path C:\users\blade19899\Desktop -Recurse | measure -Sum Length).Sum. Apparently MS has their own du available for download: docs.microsoft.com/en-us/sysinternals/downloads/du – root Oct 23 '17 at 15:07
  • @root , that gives me an error. And the du Windows equivalent is buggy, and doesn't display the folder size – blade19899 Oct 23 '17 at 15:20
  • What is the error? – root Oct 23 '17 at 15:21
  • @root Unexpected token ')' in expression or statement.. I removed the ), but it then just display count an not the subfolders. Tried it with a *, but no luck. – blade19899 Oct 23 '17 at 15:22
1

From your question, it appears you are comfortable with the Linux command line. When on Windows, you don't necessarily have to ditch that knowledge and learn new commands: instead, you can leverage what you already know and continue using the familiar bash, du, sort, sed and awk.

My suggestion is:

  1. employ cygwin or cmder or a similar tool, in order to continue doing your stuff
  2. learn Powershell to do new things

Note: sometimes a portable installation is the only way to go; in that case I'd recommend cmder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.