Problem with Powershell script to check folder sizes on remote servers

by May 13, 2014

My script below is supposed to get the total size of folders and files within acertain directory. However the error shows

 

Get-ChildItem : Cannot find path '\servername  c$Documents and Settings' because it does not exist.

note the gap after servername in path. I think this is the issue but dont know how to get round it.

$hostnames=Get-Content c:servers.txt
$directory = "c$Documents and Settings"
$FolderItems = Get-ChildItem -path "\$hostnames$directory" -recurse
$Measurement = $FolderItems | Measure-Object -property length -sum
$colitems = $FolderItems | measure-Object -property length -sum
"$hostname,$directory;{0:N2}" -f ($colitems.sum / 1MB) + "MB;" + $Measurement.count + " files;"