MSH> "Hello, World!" | Out-Clipboard
MSH> Get-Clipboard
Hello, World!
MSH>
MSH> get-process | Out-Clipboard
MSH> Get-Clipboard
System.Diagnostics.Process (ahnsd)
System.Diagnostics.Process (ahnsdsv)
...
System.Diagnostics.Process (winlogon)
System.Diagnostics.Process (wmiprvse)
MSH>
function set-clipboard {$input | out-string | out-clipboard}
MSH> set-alias installutil $env:windir\Microsoft.NET\Framework\v2.0.50727\installutilNow, ClipboardSnapin is loaded and you are good to go.
MSH> cd [go to Snapin assembly directory]
MSH> installutil Snapins.dll
MSH> get-mshsnapin -r
Name : ClipboardSnapin
MshVersion : 1.0
Description : Provides retrieving/sending data from/to Clipboard
MSH> add-mshsnapin ClipboardSnapin
using System;
using System.Text;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using System.Management.Automation;
namespace D2D.Snapins
{
/// <summary>
/// Define basic information of this snap in
/// </summary>
[RunInstaller(true)]
public class ClipboardSnapin : MshSnapIn
{
public ClipboardSnapin() : base()
{
}
public override string Name
{
get { return "ClipboardSnapin"; }
}
public override string Vendor
{
get { return "DontBotherMeWithSpam"; }
}
public override string Description
{
get { return "Provides retrieving/sending data from/to Clipboard"; }
}
}
/// <summary>
/// Cmdlet to return clipboard text to pipe
/// </summary>
/// <remarks>
/// Original Author: Keith Hill (http://spaces.msn.com/keithhill)
/// Original Source URL: http://home.comcast.net/~rkeithhill/MSH/Clipboard.cs
/// Modified by: DBMwS
/// Date: 01/31/2006 @ 12:59AM
///
/// Removed, "timeout" feature
/// </remarks>
[Cmdlet("Get", "Clipboard")]
public class GetClipboardCommand : Cmdlet
{
private Exception ex;
private string dataObject = "";
protected override void BeginProcessing()
{
Thread t = new Thread(new ThreadStart(GetObject));
try
{
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
}
catch (Exception ex)
{
ErrorRecord err = new ErrorRecord(ex, "GetClipboardError",
ErrorCategory.NotSpecified, t);
WriteError(err);
}
if (this.ex != null)
{
WriteError(new ErrorRecord(this.ex,
"GetClipboard::GetObjectError",
ErrorCategory.NotSpecified, null));
}
WriteVerbose(string.Format("Sending '{0}' to pipe",
this.dataObject.ToString()));
WriteObject(this.dataObject,
(this.dataObject.GetType().IsArray));
}
private void GetObject()
{
try
{
this.dataObject = Clipboard.GetText();
}
catch (Exception ex)
{
this.ex = ex;
}
}
}
/// <summary>
/// send output string to clipboard
/// </summary>
[Cmdlet("Out", "Clipboard")]
public class OutClipboardCommand : Cmdlet
{
/// <summary>
/// Unhandled exception
/// </summary>
private Exception ex;
/// <summary>
/// Actuall worker thread
/// </summary>
private Thread t;
/// <summary>
/// Holds all the necessary input data to send to Clipboard
/// </summary>
private StringBuilder sb = new StringBuilder();
/// <summary>
/// Input data received either from pipe or as an argument
/// </summary>
private string []inputData;
/// <summary>
/// Input data to send to clipboard
/// </summary>
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
public string []InputData
{
get { return inputData; }
set { inputData = value; }
}
/// <summary>
/// Create a STA thread to start later on.
/// </summary>
protected override void BeginProcessing()
{
WriteVerbose("Begin Processing(Creating a new STA thread)...");
t = new Thread(new ThreadStart(SetObject));
t.SetApartmentState(ApartmentState.STA);
}
/// <summary>
/// Append all input lines into one
/// (with a newline appended for each input data)
/// </summary>
protected override void ProcessRecord()
{
WriteVerbose("Processing Record...");
try
{
if (this.InputData != null)
{
WriteVerbose(
string.Format("\tAppending {0}", this.InputData));
foreach (string line in this.InputData)
{
this.sb.AppendLine(line);
}
}
}
catch (Exception ex)
{
// Well I don't know what happened,
// so send the error to the host...
ErrorRecord err = new ErrorRecord(ex, "OutClipboardError",
ErrorCategory.NotSpecified, t);
WriteError(err);
}
}
/// <summary>
/// Start a thread to send input data to Clipboard
/// </summary>
protected override void EndProcessing()
{
try
{
t.Start();
t.Join();
}
catch (Exception ex)
{
ErrorRecord err = new ErrorRecord(ex, "OutClipboardError",
ErrorCategory.NotSpecified, t);
WriteError(err);
}
// Handle error occurred while sending text to clipboard
// Well, try out something as stupid as "clear-host | out-clipboard"...
if (this.ex != null)
{
WriteError(new ErrorRecord(this.ex,
"OutClipboard::SetObjectError",
ErrorCategory.NotSpecified, null));
}
WriteVerbose("End Processing...");
}
/// <summary>
/// Send input data to clipboard
/// </summary>
private void SetObject()
{
try
{
// Before sending the inputData to clipboard,
// we need to drop the last newline character
string text = this.sb.ToString();
text = text.Substring(0,
(text.Length - System.Environment.NewLine.Length));
Clipboard.SetText(text);
}
catch (Exception ex)
{
this.ex = ex;
}
}
}
}
Experimenting with a different format of blogs...