This topic contains 12 replies, has 6 voices, and was last updated by
February 23, 2014 at 1:16 pm #13289
I am new in Power shell. I will appreciate if somebody answer my question.
I am going to get the size of directory ( considering subdirectories , files and folders ) . Here the command which I used:
"get-childitem -Path c:\windows\temp -recurse | measure-object -property Length -sum | select-object Count, Sum"
but I think the size of directories with this command is a little less that size of my temp folder when I click on it with mouse and see properties. Does this command consider directory size ? or it just give me size of files on "temp" folder and its subdirectories?
Does "count" just show the number of files? How can I get number of files and folders?
Sorry for my English,
February 23, 2014 at 1:20 pm #13290KeymasterPoints: 1,785Rank: Community Hero
Directories don't have a size. The "size" of a directory is the sum of the sizes of the files contained in the directory. So the command is only giving you file sizes – because that's the only thing it can.
February 23, 2014 at 1:25 pm #13291
If you have any hidden or System files within that directory tree, your current command isn't taking them into account. Try adding the -Force parameter to Get-ChildItem.
Count will be showing both files and folders, as written, but as Don mentioned, only files will contribute to the Sum column in your output.
February 24, 2014 at 9:56 am #13301
Don, and Dave, Thanks for precious information. It worked:)
February 24, 2014 at 11:54 am #13311
I am going to put my powershell scrip in batch file ( here is my batch file : c:\windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -Command "Get-childitem –Path c:\windows\temp –recurse –Force | measure-object –property Length –Sum | select-object Count, Sum" > c:\myfolder\output.txt ).
When I run my command ( Get-childitem –Path c:\windows\temp –recurse –Force | measure-object –property Length –Sum | select-object Count, Sum > c:\myfolder\output.txt ) in powershell window, it works, but it does not work from the batch file. I worked on it several hours. Can somebody tell me why it does not work from batch file?
Sorry for my English , and thanks for your help!
February 24, 2014 at 1:35 pm #13315ParticipantPoints: 0Rank: Member
Have you tried including '> C:\MyFolder\Output.txt' in the double quotes as well? Or even using Out-File to create the log? Something like this:
c:\windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -Command “Get-childitem –Path c:\windows\temp –recurse –Force | measure-object –property Length –Sum | select-object Count, Sum | Out-File C:\MyFolder\Output.txt”
If I had to guess Powershell is putting out the results in its 'session' but it isn't being returned back to the command prompt 'session' since you're running the redirection outside of the shell. Hopefully that makes sense.
February 24, 2014 at 2:05 pm #13316
Thanks Wilson, it works with double quote when I run the whole script on cmd line, but from batch file it wont create output file. Please take a look my attached command line file. I tried several format, but all don't work from the batch file.
Again thanks for your solution!!
February 24, 2014 at 7:19 pm #13319
You might have an easier time using powershell.exe's -EncodedCommand parameter; it was designed for situations like this, where quotation marks or special characters cause problems when trying to launch PowerShell from something like a batch file or command prompt. For instance, you can open up PowerShell and do this:
$command = "Get-childitem –Path c:\windows\temp –recurse –Force | measure-object –property Length –Sum | select-object Count, Sum | Out-File C:\marjan\output.txt" $encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command)) $encodedCommand $encodedCommand | clip.exe
At that point, the base64 encoded command will be on your clipboard, and you can paste it into your batch file. You'd launch PowerShell.exe like this:
PowerShell.exe -EncodedCommand RwBlAHQALQBjAGgAaQBsAGQAaQB0AGUAbQAgABMgUABhAHQAaAAgAGMAOgBcAHcAaQBuAGQAbwB3AHMAXAB0AGUAbQBwACAAEyByAGUAYwB1AHIAcwBlACAAEyBGAG8AcgBjAGUAIAB8ACAAbQBlAGEAcwB1AHIAZQAtAG8AYgBqAGUAYwB0ACAAEyBwAHIAbwBwAGUAcgB0AHkAIABMAGUAbgBnAHQAaAAgABMgUwB1AG0AIAB8ACAAcwBlAGwAZQBjAHQALQBvAGIAagBlAGMAdAAgAEMAbwB1AG4AdAAsACAAUwB1AG0AIAB8ACAATwB1AHQALQBGAGkAbABlACAAQwA6AFwAbQBhAHIAagBhAG4AXABvAHUAdABwAHUAdAAuAHQAeAB0AA==
February 25, 2014 at 6:04 am #13326ParticipantPoints: 14Rank: Member
wow that's cool Dave!
February 25, 2014 at 9:36 am #13334
Thanks Dave, this is more complicated command. I run base64 encoded command in PowerShell, then I paste it in my batch file, Then I run it as an administrator, but still doesn't work. Please take a look in my attached files. What is the problem?
Thanks for your time ,and your answer!
February 25, 2014 at 9:41 am #13337
It's hard to tell from the screenshot, but it looks like you've inserted at least one extra line break in the middle of the base-64 encoded command. That call to PowerShell.exe needs to all be on a single line of the batch file. Try turning off Word Wrap (from the Format menu of Notepad) to make sure it's all on one line.
February 25, 2014 at 10:06 am #13338
Thank you thank you Dave, Finally it worked:)
January 25, 2015 at 11:48 pm #22126ParticipantPoints: 1Rank: Member
People looking for a script to find folder sizes who are struggling with max path length limitations, or slowness with Get-ChildItem, might want to check out the script that I put here, which uses robocopy under the hood, and is very fast. Faster than Get-ChildItem – and it'll work on paths longer than 260 characters. No more "path too long" exceptions messing up the numbers for your reports.
Here it is:
Get Folder Size with PowerShell, Blazingly Fast (powershelladmin.com).
The topic ‘get Directory Size’ is closed to new replies.