Windows – Powershell – email free space information

emailpowershellwindows

i am currently using:

$emailFrom = "user@host.com"
$emailTo = "destination@host.com"
$subject = "subject"
$body = "message"
$smtpServer = "mail.host.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

and i know that with the folowing command i can find out the free space on all of my hdd:

Get-WmiObject WIN32_logicaldisk | sort -desc freespace | select -first 3 | format-table -autosize deviceid,devicetype,providername,freespace,size,volumename;

when i try to do this:

$body = Get-WmiObject WIN32_logicaldisk | sort -desc freespace | select -first 3 | format-table -autosize deviceid,devicetype,providername,freespace,size,volumename;

but all i get in my email is the folowing:

    Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData 
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData

how can i get my free space via email using powershell???

Best Answer

Try:

$body = Get-WmiObject WIN32_logicaldisk | sort -desc freespace | select -first 3 | format-table -autosize deviceid,devicetype,providername,freespace,size,volumename | out-string

I've just tried it on my PC and it worked.

For some background on why this works see:

http://blogs.msdn.com/powershell/archive/2006/04/25/how-does-select-string-work-with-pipelines-of-objects.aspx

JR