Creating a form with .NET library in PowerShell

Below is an example of calling .NET library with PowerShell to build a form. The form will have a textbox for user to input a computer name to ping.

Web link reference included in the comment

#region Boring beginning stuff
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
#region begin to draw forms
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Computer Pinging Tool"
$Form.Size = New-Object System.Drawing.Size(300,170)
$Form.StartPosition = "CenterScreen"
$Form.KeyPreview = $True
$Form.MaximumSize = $Form.Size
$Form.MinimumSize = $Form.Size
$label = New-Object System.Windows.Forms.label
$label.Location = New-Object System.Drawing.Size(5,5)
$label.Size = New-Object System.Drawing.Size(240,30)
$label.Text = "Type any computer name to test if it is on the network and can respond to ping"
$textbox = New-Object System.Windows.Forms.TextBox
$textbox.Location = New-Object System.Drawing.Size(5,40)
$textbox.Size = New-Object System.Drawing.Size(120,20)
#$textbox.Text = "Select source PC:"
$ping_computer_click =
#region Actual Code
$statusBar1.Text = "Pinging..."
$ComputerName $textbox.Text
if (Test-Connection $ComputerName -quiet -Count 2){
Write-Host -ForegroundColor Green "Computer $ComputerName has network connection"
$result_label.ForeColor= "Green"
$result_label.Text = "System Successfully Pinged"
Write-Host -ForegroundColor Red "Computer $ComputerName does not have network connection"
$result_label.ForeColor= "Red"
$result_label.Text = "System is NOT Pingable"
$statusBar1.Text = "Testing Complete"
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(140,38)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$result_label = New-Object System.Windows.Forms.label
$result_label.Location = New-Object System.Drawing.Size(5,65)
$result_label.Size = New-Object System.Drawing.Size(240,30)
$result_label.Text = "Results will be listed here"
$statusBar1 = New-Object System.Windows.Forms.StatusBar
$statusBar1.Name = "statusBar1"
$statusBar1.Text = "Ready..."
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){& $ping_computer_click}})
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
#endregion begin to draw forms
#Show form
$Form.Topmost = $True
[void] $Form.ShowDialog()

[PowerShell] Send email from Gmail with attachment on PowerShell

Credits goes to 

I modified the script a little bit to pick up the log from c:\temp\ daily where the filename is made up of yyyymmdd

$a = Get-Date -Format yyyyMMdd

$smtpClient = new-object 
$smtpClient.Host = ''
$smtpClient.Port = 587
$smtpClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("", "password");

$emailFrom = ""
$emailTo = ","
$Subject = "Backup log for " +$a
$Body = "Please refer to attached logfile."

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = $EmailFrom
$emailMessage.Subject = $Subject
$emailMessage.Body = $Body
$emailMessage.Attachments.Add("C:\temp\libra_backup"+ $a+".txt")


For more information on Get-Date format, please click here

[PowerShell] Retrieving hard disk health through SMART status

To get the SMART status of the hard disk with PowerShell, you could run the following:

Get-WmiObject win32_diskdrive|Select-Object Status


Below is a sample script to check multiple PC’s SMART status by getting a list of PC names from a text file.

$allComputers=Get-Content -Path C:\temp\pc.txt

foreach($computer in $allComputers)

$isonline=test-connection -Quiet -count 2 -ComputerName $computer

$diskStatus= Get-WmiObject win32_diskdrive -ComputerName $computer|Select-Object -ExpandProperty Status

Write-Host -ForegroundColor Green "$computer is online and disk status is $diskStatus";
Write-Host -ForegroundColor Red "$computer is offline";


WMI Explorer for those who are interest in WMI programming

For those of you who are interest in WMI programming, tools like WMI code Creator and Scriptomatic can be useful. The command wbemtest might come in handy sometimes as well but I find that WMI Explorers are more useful when you would like to know what classes are avaiable and when you would like to run and see what results does the Query produces.

I find the following three WMI Explorer to be very useful:

Happy programming 🙂