Using powershell get size of folder and subfolders on Windows Server 2012 R2

Copper Contributor

I created script below and I now want to enhance it as I am getting errors.

# Create the filename with the current date
$filename = ([string]::Format("\\JHBDSM020000128\QlikView\Elijah\Data\Detailed\128_Detailed_FileSystem_{0}.csv", (Get-Date).toString("yyyyMMdd")))
# Delete the file if exists
if([System.IO.File]::Exists($filename)){
# file with path $path exist
Remove-Item -path $filename
}
$Drives = Get-PSDrive -PSProvider 'FileSystem'
foreach($Drive in $drives) {
# Delete the file if exists
Get-ChildItem -Path $Drive.Root -recurse | Select-Object Directory, Name, LastWriteTime, Length | Out-File -Append $filename -NoClobber
}

 

Attached is the result set I am getting. If you look at attached you'll notice that only Directory and Name fields are showing but the other fields are not visible.

 

Can someone please assist as I am using the same script on other servers but the servers are Windows Server 2003 R2 and the result set is working perfectly.

 

Please also don't forget adding the script to show Folder path and size.

 Resultset_Powershell.JPG

1 Reply

You're problem has to do with the output width length.  By default, it is truncating due to its nature.  A better way is to use a format that preserves data.  Either clixml, csv, or json.  Here's an example of collecting all of the results in a $stuff array, and then exporting it to CSV:

$stuff=@()
foreach($Drive in $drives) {
    $stuff += Get-ChildItem -Path $Drive.Root -recurse | Select-Object Directory, Name, LastWriteTime, Length 
}
$stuff |export-csv -nottypeinformation -encoding ASCII $filename