Recommendations on how to tie a bunch of Get- to an output .txt
Solution 1:
First thing I noticed is that you are counting the files sizes of files directly inside folders, but nothing that is stored in subfolders. You only use recursion on the Recycle Bin..
Secondly, you are repeating the code to format the size in every step, so I would suggest creating a small helper function for that to make the code much cleaner.
Then you hardcode the folder paths where I think there are better options for that and lastly, if your aim is to output 7 formatted numbers to a file, why not create a CSV file from the gathered info, so you have headers to tell you what each number stands for.
Try below:
function Format-ByteSize {
# helper function to format a given size in bytes
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateRange(0, [double]::MaxValue)]
[double]$SizeInBytes
)
$units = "Bytes", "KB", "MB", "GB", "TB", "PB"
$index = 0
while ($SizeInBytes -gt 1024 -and $index -le $units.length) {
$SizeInBytes /= 1024
$index++
}
if ($index) { '{0:N2} {1}' -f $SizeInBytes, $units[$index] }
else { "$SizeInBytes Bytes" }
}
#Check size of C and if its < or = 20GB
$FreeSpace = (Get-PSDrive -Name 'C').Free
if ($FreeSpace -le (20 * 1GB)) {
Write-Warning "The C Drive's free space is less or equal to 20GB"
}
# get the sizes of the folders of interest.
$desktopPath = [Environment]::GetFolderPath("Desktop")
$DesktopSize = (Get-Childitem -Path $desktopPath -File -Recurse | Measure-Object -Sum Length).Sum
# seeing that the 'My Pictures', 'My Music' and 'My Videos' folders are INSIDE the 'My Documents' folder, you may
# want to think about adding the -Recurse switch to this path or not..
$DocumentSize = (Get-Childitem -Path ([Environment]::GetFolderPath("MyDocuments")) -File | Measure-Object -Sum Length).Sum
# If you've recursed the 'My Documents' folder, the below sizes will be included there
$PicturesSize = (Get-Childitem -Path ([Environment]::GetFolderPath("MyPictures")) -File -Recurse | Measure-Object -Sum Length).Sum
$MusicSize = (Get-Childitem -Path ([Environment]::GetFolderPath("MyMusic")) -File -Recurse | Measure-Object -Sum Length).Sum
$VideoSize = (Get-Childitem -Path ([Environment]::GetFolderPath("MyVideos")) -File -Recurse | Measure-Object -Sum Length).Sum
# the Downloads folder path is more tricky to find
$downloadsPath = (New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').Self.Path
$DownloadSize = (Get-Childitem -Path $downloadsPath -File -Recurse | Measure-Object -Sum Length).Sum
# finally the recycle bin on the C drive
$RecycleBin = (Get-ChildItem -LiteralPath 'C:\$Recycle.Bin' -File -Force -Recurse -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum).Sum
# now put all this info in an **object** so you can save as CSV file with headers
$result = [PsCustomObject]@{
FreeSpace = Format-ByteSize $FreeSpace
Desktop = Format-ByteSize $DesktopSize
MyDocuments = Format-ByteSize $DocumentSize
MyPictures = Format-ByteSize $PicturesSize
MyVideos = Format-ByteSize $MusicSize
Downloads = Format-ByteSize $DownloadSize
RecycleBin = Format-ByteSize $RecycleBin
}
# output to console screen
$result
#output to structured CSV file you can open in Excel
$result | Export-Csv -Path (Join-Path -Path $desktopPath -ChildPath 'CDriveFullOutput.csv') -NoTypeInformation -UseCulture
On screen, this will output something like this:
FreeSpace : 119,49 GB
Desktop : 12,23 KB
MyDocuments : 26,99 MB
MyPictures : 1,88 GB
MyVideos : 119,90 GB
Downloads : 573,88 MB
RecycleBin : 537 Bytes