How to Sum All DB Sizes for an Instance with Powershell?

# Option 1 - be aware of the $TotalSize is not reset after each execution.
PS SQLSERVER:\SQL\XU-ACER-PC\DEFAULT\Databases> dir | %{$TotalSize +=$_.Size}
PS SQLSERVER:\SQL\XU-ACER-PC\DEFAULT\Databases>$TotalSize

# Option 2 - create a function as below to initialize the $TotalSize:

function TestReset
{
$TotalSize=0
cd SQLSERVER:\SQL\XU-ACER-PC\DEFAULT\Databases
dir | %{$TotalSize +=$_.Size}
$TotalSize
}

# save it in C:\users\XU-Acer\Documents as Test.ps1
# run it as below:
PS C:\users\XU-Acer\Documents> . .\Test.ps1
# Then run the function as below. It always shows the same number.
PS C:\users\XU-Acer\Documents> testreset
# 927.8125

# Option 3 - you can manipulate $a
PS SQLSERVER:\SQL\XU-ACER-PC\DEFAULT\Databases> $a=dir | measure -prop Size -sum
PS SQLSERVER:\SQL\XU-ACER-PC\DEFAULT\Databases> $a.sum/1kb # make the default MB to GB

# Others - If you want to filter database
PS SQLSERVER:\SQL\XU-ACER-PC\DEFAULT\Databases> $a=(dir | ? {$_.name-eq "AdventureWorks2014"} | measure -prop size -sum)