How to get powershell output in form of a table

Ashima Gupta 66 Reputation points
2020-11-04T06:41:26.203+00:00

Hi guys,

I've one new requirement how can we get our output in a table format and send a mail of that table format in powershell.

Foreach ( $user_dir in ( Get-ChildItem "C:\Users" )) {
$Profile_Size = (Get-ChildItem -Recurse -Force -ea silentlycontinue -Path "$($user_dir.FullName)" | Measure-Object length -sum).sum
$sizemb = $Profile_Size / 1MB
$username = $($user_dir.Name)
$sizemb = [math]::round($sizemb,2)
if ($sizemb -gt 500)  {
$userlist = $username
Add-Content D:\tessssttttt2\output.txt "$userlist uses $sizemb"
}
}
$smtpServer = "---------------" 
$smtpFrom = "-----------" 
$smtpTo = "--------------" 
$messageSubject = "Disk Utilization of C Drive by users" 
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto 
$message.Subject = $messageSubject 
$message.IsBodyHtml = $true
$message.Body = "<p>C drive utiliazation<p>"
$message.Body += Get-Content D:\tessssttttt2\output.txt | ForEach-Object {
$message.Body +="$_<br/>" }
$smtp = New-Object Net.Mail.SmtpClient($smtpServer) 
$smtp.Send($message)

This is my script which I'm using can someone guide me how can I get the email with table format.

And I want to schedule this script on daily basis so that Add-Content is appending the users in previous file only I want it will delete the pervious file and then we get the new output file.

Any help on this would be appreciated.

Thanks in advance

Regards

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Viktor Dronov 146 Reputation points
    2020-11-04T07:50:44.537+00:00

    1 at the wery beggining you could remove your file, so at the end you will get new one created
    2 take a look on convertto-html cmdlet instead of using your foreach {"$_</br>> construction
    3 line 7 doing nothing
    4 instead of writing/reading file you can just use variable to store your results. In this case you will not be needed remove anything

    $userlist =@()
    Foreach ( $user_dir in ( Get-ChildItem "C:\Users" )) {
    $Profile_Size = (Get-ChildItem -Recurse -Force -ea silentlycontinue -Path "$($user_dir.FullName)" | Measure-Object length -sum).sum
    $sizemb = $Profile_Size / 1MB
    $sizemb = [math]::round($sizemb,2)
    if ($sizemb -gt 500) {
    $userlist += [pscustomobject]@{
    ProfileName = $user_dir.Name
    ProfileSizeMB = $sizemb
    }
    }
    }
    $body = $userlist | ConvertTo-Html # more info you can find in a help and in attached link

    https://4sysops.com/archives/building-html-reports-in-powershell-with-convertto-html/

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.