Posts Tagged ‘Powershell Scripting’

Send email with Powershell

There’s two methods I often use for sending email in my Powershell scripts. The first uses the Send-MailMessage cmdlet and the second resorts to using the .Net Framework. Why do I use two methods? Well…. Send-MailMessage -To ‘’ -From ‘’ -Subject ‘Test Message’ -Body ‘This is a test message.’ -SmtpServer ‘’; The Send-MailMessage cmdlet is […]

Console input with Powershell

I’m looking at building some Powershell scripts that can accept user input to perform different tasks with a wizard style interface. As it happens this is fairly easily achieved with the Read-Host cmdlet. Here’s a quick script showing how such a powershell script may look. function mainMenu() { Clear-Host; Write-Host “============”; Write-Host “= MAINMENU =”; […]

Managing Index Fragmentation with Powershell

Here’s a Powershell script that can be used to manage index fragmentation in SQL Server databases. The strategy I’ve used in the script is based on a recommendation from Pinal Dave (blog | twitter) in his article  Difference Between Index Rebuild and Index Reorganize Explained with T-SQL Script. Just set the $sqlserver and $database variables […]

Powershell Random sort

Here’s a Powershell snippet that randomly sorts the lines in a file. The snippet below reads a text file, called file.txt, located in your user profile directory. The data in the file will be written back with the lines in a different order. $data = New-Object System.Object; $data = Get-Content “$Env:USERPROFILE\file.txt”; # Random sort the […]

Using .Net libraries in Powershell

One of the great things about Powershell is its ability to take advantage of the .Net platform. If Powershell can’t do it you bet there’s a .Net library that can. Not only is this great for extending your scripts but I also like to use Powershell to test some of my classes. Rather than fire […]