I’m editing a script (Disk cleaner) and want to output at the end of my script all the space that was cleared and how much space remains in the disk. I’ve written everything and it all works, however I want my script to display the message of how much was cleared and remains to a text file that can be retrieved rather than displaying it how I am at the moment (through the use of write-host). I did some searching and discovered that add-content works, although I’m unsure of how to do this since the script is intended to be run on various virtual servers so I’m unaware of any paths that may be needed.
Here is a small snippet of my code to show:
$disks = Get-WMIObject -Computer $server -Class Win32_LogicalDisk -Filter "DeviceID like '%C%'"
$beforeFreeSpace = $disks.FreeSpace
$beforeFreeSpaceMB = [math]::truncate($beforeFreeSpace / 1MB)
write-output $beforeFreeSpaceMB
#Lots of code right here in between
$afterFreeSpace = $disks.FreeSpace
$afterFreeSpaceMB = [math]::truncate($afterFreeSpace / 1MB)
$freedSpace = $afterFreeSpaceMB - $beforeFreeSpaceMB
write-output $freedSpace
write-output "Free space remaining after cleanup (in MB): " $afterFreeSpaceMB
Pretty much I want to find a way to get the outputs in a file that is easily retrievable. Is add-content the best method of doing this or is there an easier/more effective way to accomplish this goal?
Thanks!
Instead of using Write-Output you could just do something like this:
Another, probably cleaner way of doing this would be to log things like this –