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…. ?View Code POWERSHELLSend-MailMessage -To ‘me@here.com’ -From ‘sender@here.com’ -Subject ‘Test Message’ -Body ‘This is a test message.’ -SmtpServer ‘smtp.server.com’; The Send-MailMessage […]

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. ?View Code POWERSHELLfunction mainMenu() { Clear-Host; Write-Host "============"; Write-Host "= […]

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. ?View Code POWERSHELL$data = New-Object System.Object; $data = Get-Content "$Env:USERPROFILE\file.txt"; # Random […]

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 […]