Integer Variable Not Increasing When Provided with Values

Please can anyone help with this issue.

I’m trying to calculate the size of the homedrive for each user in an AD Group and then add it to a variable to keep a running total of the homedrive sizes. I want the size of the each users homedrive and the running total size of all mailboxes to be displayed as the search progresses. I have written the following:

[int]$TotalSizeofAllHomeDrives = 0 

$ADUser = (get-aduser -SearchBase "ExampleADGroup" -filter *).samaccountname

Foreach ($User in $ADUser){

$SizeofUserHomeDrive = ((get-aduser -id "$User" -properties homedirectory).homedirectory |
Get-ChildItem -recurse | measure-object -property length -sum).sum

$SizeofUserHomeDrive += $TotalSizeofAllHomeDrives

Write-Host "The size of the Home Drive for $User is $SizeofUserHomeDrive" 
Write-Host "Current total of all Home Drives is $TotalSizeofAllHomeDrives"

 }

As the search runs, the size of each users home drive is being displayed but the 'Current total of all Home Drives (the $TotalSizeofAllHomeDrives figure) always displays as 0.

line 10 should read

$TotalSizeofAllHomeDrives += $SizeofUserHomeDrive

:slight_smile:

Thanks for the input. I’ve changed the line as suggested.

It now seems to update the $TotalSizeofAllHomeDrives based on the first users $SizeofUserHomeDrive but then doesn’t update it any further for subsequent users, even though their $SizeofUserHomeDrive are being returned.

I’ve changed the output to GB and it’s started to work - maybe didn’t like the large numbers?

I’ve encountered another issue where, for some user folders, I get ‘An unexpected network error has occured’. The results are being output for each user without issue until I get one of these error messages but then I get no more outputs for any users even though the script continues to run.