This monthâ€™s T-SQL Tuesday is brought to us by Wayne Sheffield (blog | twitter), who asks for some awesome uses of Powershell to manage SQL Server.
As with most scripts, I had a flash of inspiration that I could use Powershell to make my life a little bit easier. I spent 2 hours to save 10 seconds of effort, but it keeps saving me often. Six months ago, I needed to sort a list of server names. Nothing too exciting, and most people would fire up Excel to perform the sort.
You would first copy your unsorted list into the clipboard, paste it into Excel, highlight the rows (or the entire column) and hit â€œSortâ€. Then youâ€™d select the newly sorted rows, copy it back into the clipboard, and paste it elsewhere.
Thatâ€™s way too much effort.
Letâ€™s try this workflow instead: You copy your unsorted list. You click on an icon in your taskbar, watch a Powershell window flash up for half a second, and then you paste your clipboard, noting that the results are sorted.
The script isnâ€™t too long, only complicated by the fact we need to hook into System.Windows.Forms to gain access to the clipboard:
## This will only work if you run Powershell with the -sta switch:
# Powershell -sta
Add-Type -AssemblyName System.Windows.Forms
$tb = New-Object System.Windows.Forms.TextBox
$tb.Multiline = $true
# Load System.Windows.Forms assembly.
$null = [Reflection.Assembly]::LoadWithPartialName(â€œSystem.Windows.Formsâ€)
$dataObject = New-Object windows.forms.dataobject
# Add generated strings to data object.
$dataObject.SetData([Windows.Forms.DataFormats]::UnicodeText, $true, $text)
$data = Get-ClipboardText;
$data = $data.Replace("`r`n", "|||||||")
$data.Split("|||||||") | Sort-Object | Set-Variable -Name sorted
[string] $output = ""
foreach ($line in $sorted)
if ($line -ne "")
$output += $line + "`r`n"
Iâ€™m sure there must be an easier way to perform the sort than my 10 line solution, but this was the best solution I was able to come up with in the limited time I allowed myself.
In order to set this up as an icon, we simply create a shortcut with the target:
powershell.exe -sta ./Sort-Clipboard.ps1
making sure to â€œStart Inâ€ the path with the Sort-Clipboard.ps1 file. The â€“sta flag is necessary to run Powershell in Single Threaded Apartment mode, in order to gain access to the Clipboard class.
If you want to use the Sort-Clipboard.ps1 script from within Powershell, youâ€™ll need to spin up a new Powershell session in â€“sta mode, which can be done using a simple one-line script:
Start-Process powershell -args "-sta -noprofile .\Sort-Clipboard.ps1"
Credit and code for these techniques must go to Brian Reiter, Dave Regal, and the MSDN Powershell blogging team. Thank you all!
Server names are one example, but thereâ€™s many more situations where you might want to sort a list in day-to-day database management. You could adapt the code to sort anything â€“ the really useful part here is the access to the clipboard.