Get size folders from file

Hello there,

I want to get total folder size with PWSH (path of folder are on file),

The content of file.txt :

“C:\Program Files”,“C:\Program Files (x86)”

Her the code :

$file = get-content "C:\temp\file.txt"
"{0:N2} MB" -f ((Get-ChildItem $file -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB)

PWSH return :

Get-ChildItem : Impossible to find « C:\Program Files C:\ », doesn't exist.

What’s wrong ? :confused:




Get-Content is an array of lines. If you defined an array like this in your code in-line:

$tmp = “C:\Program Files”,”C:\Program Files (x86)”

If you want to import from a file directly, normally you would separate each path on a different line:

C:\Program Files
C:\Program Files (x86)

The goal is creating an array of paths. You can parse the line in the file with split and replace, but it.'s simpler to just format the content in the file

You say the content of the file is “C:\Program Files”,“C:\Program Files (x86)” but it is either your quotes are not valid quotes (try replacing them by typing them manually) or the are no quotes. Either of those options will lead to the error you displayed. You could also store them on on each line and use Import-Csv like this.

$tempfile = New-TemporaryFile

C:\Program Files
C:\Program Files (x86)
'@ | Set-Content $tempfile -Encoding utf8

$file = Import-Csv $tempfile -Header path

"{0:N2} MB" -f ((Get-ChildItem $file.path -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB)

You are also likely to have some items return “access is denied” errors, to keep the output neat you could use the parameter -ErrorAction SilentlyContinue on

command. If you wanted to see what paths return error while keeping the output neat, then add
-ErrorVariable somevariablename
like this
"{0:N2} MB" -f ((Get-ChildItem $file.path -Recurse -ErrorAction SilentlyContinue -ErrorVariable errors | Measure-Object -Property Length -Sum).Sum / 1MB)

@Rob thanks for clarifying that. My testing was misleading as this works

Get-ChildItem "C:\Program Files","C:\Program Files (x86)" | select parent -Unique

But this doesn’t

$tempfile = New-TemporaryFile

"C:\Program Files","C:\Program Files (x86)"
'@ | Set-Content $tempfile -Encoding utf8

Get-Content $tempfile -OutVariable file

Get-ChildItem $file | select parent -Unique

Thanks @Rob-Simmers -and @KrzyDoug, very helpful !

Ticket solved, thanks a lot !