::::::::: PowerShell :::::::::
Saturday, January 28, 2006
  [MSH Snapin] Need help implementing Out-Clipboard Snapin
Well, while playing around with new MSH beta 3 feature, MSH Snapin, I came across with /\/\o\/\/'s MSH Clipboard use workaround blog.

Since, Msh engine is created using Muti-Thread Apartment model, it's still not possible to create or use an object that requires STA. /\/\o\/\/'s solution uses in-memory compilation and dynamic object instantiation using CodeDom namespace.

Well, since we have a new Snapin feature, I have decided to create my own "out-clipboard" cmdlet.

In the topic i have mentioned that, there is some problems with the Snapin. I can't seem to get an input to be passed from pipe although I have specified "ValueFromPipeline=true" in "Parameter" attribute. Oh well, it is far from being complete, but posting about this code to ask about the problem and how I should be improving it at usenet or at forums...

So far, only sending text to clipboard works...

MSH>Out-Clipboard "Hello, World"
Begin Processing...
Processing Record...
Setting String
End Processing...
MSH>"Hello, World" | Out-Clipboard
Begin Processing...
Processing Record...
Setting MshObject
End Processing...

I have pasted "Hello, World" in the second MSH prompt through mouse and then piped the data to "out-clipboard". The first case works while the latter case doesn't as I have mentioned above...

Comment on source:
This snapin basically creates a new STA thread and send input argument to clipboard through callback.

using System;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;
using System.Management.Automation;
using System.Collections.Generic;
using System.Text;

namespace snapins
public class OutClipboardSnapin : System.Management.Automation.MshSnapIn
public OutClipboardSnapin() : base()

public override string Name
get { return "OutClipboardSnapin"; }

public override string Vendor
get { return "DontBotherMeWithSpam"; }

public override string Description
get { return "This snapin receives piped output into Clipboard"; }

[Cmdlet("Out", "Clipboard")]
public class OutClipboardCommand : Cmdlet
private Thread t;
private object inputData;
/// <summary>
/// Input data to send to clipboard
/// </summary>
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline=true)]
public object InputData
get { return inputData; }
set { inputData = value; }

protected override void BeginProcessing()
t = new Thread(new ThreadStart(SetObject));

WriteObject("Begin Processing...");

protected override void ProcessRecord()
WriteObject("Processing Record...");
WriteObject("Setting " + this.InputData.GetType().Name);

protected override void EndProcessing()
WriteObject("End Processing...");

private void SetObject()
if (this.InputData.GetType() == typeof(string))

Tags :
You should not take Object as input but String,

private string inputData;

/// Input data to send to clipboard

[Parameter(Mandatory = true, Position = 0, ValueFromPipeline=true)]
public string InputData

then it works,

you can see in in your output,
your are putting out the MSHObject
to the clipBoard.

gr /\/\o\/\/
PS Also you need so collect all the text first before putting it to the clipboard.

try this :

gps | out-string | out-clipboard
GPS | out-Clipboard.
Ah, great stuff. I didn't know that i had to use "string" for input instead of plain "object"...

Thanks for the help there...

It seems to be working now although I have to modify the Snapin a bit since something like "ls | out-clipboard" doesn't work. Well error I get is

MSH> ls | Out-Clipboard
Out-Clipboard : Thread is running or terminated; it cannot restart.
At line:1 char:18
+ ls | Out-Clipboard <<<<

But I don't think it'd be much of a problem to fix that bug :)
Post a Comment

Links to this post:

Create a Link

<< Home
Let's get lazy with PowerShell!

Location: Flushing, NY, United States

Experimenting with a different format of blogs...

10/01/2005 - 11/01/2005 / 11/01/2005 - 12/01/2005 / 12/01/2005 - 01/01/2006 / 01/01/2006 - 02/01/2006 / 02/01/2006 - 03/01/2006 / 03/01/2006 - 04/01/2006 / 04/01/2006 - 05/01/2006 / 05/01/2006 - 06/01/2006 / 06/01/2006 - 07/01/2006 / 07/01/2006 - 08/01/2006 / 08/01/2006 - 09/01/2006 / 10/01/2006 - 11/01/2006 / 11/01/2006 - 12/01/2006 /

Powered by Blogger