# Wednesday, October 14, 2009

Updated: now use a temporary file to set text to avoid overflowing command-line buffer

The Windows Clipboard – accessible via System.Windows.Forms.Clipboard – requires an STA thread to read/write to it. By default, the console version of PowerShell 2.0 (i.e. not ISE) starts in MTA mode. This means that read/writing via this class is unreliable. Rather than always starting up console PowerShell in STA mode via the –STA flag, you can use this flag in a sneakier way to get what you want:

function Set-ClipboardText {
        param($text)

        # need to use temp file to avoid exceeding command-line length limit
        $temp = [io.path]::GetTempFileName()

        try {
            set-content -Path $temp -Value $text

            $command = {
                    add-type -an system.windows.forms
                    [System.Windows.Forms.Clipboard]::SetText((get-content $args))
            }
            
            powershell -sta -noprofile -command $command -args $temp

        } finally {
            if ((test-path $temp)) {
                remove-item $temp
            }
        }
}

function Get-ClipboardText {
        $command = {
                add-type -an system.windows.forms
                [System.Windows.Forms.Clipboard]::GetText()
        }
        powershell -sta -noprofile -command $command
}

Essentially we are running PowerShell as a child process temporarily in STA mode, skipping loading the profile and executing a scriptblock.

posted on Wednesday, October 14, 2009 11:53:35 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] Trackback
Related posts:
PowerShell 2.0 – Partial Application of Functions and Cmdlets
PowerShell – The Patchwork of Paths, PSPaths and ProviderPaths
PowerShell - Why are keys in Hashtables sorted randomly?
PowerShell 2.0 - About Dynamic Parameters
PowerShell 2.0 – Introducing the PModem File Transfer Protocol
PowerShell 2.0 - Enabling Remoting with Virtual XP Mode on Windows 7

Referred by:
http://stackoverflow.com/questions/1567112/convert-keith-hil... [Referral]
http://www.google.com/reader/view/ [Referral]
http://www.google.nl/ig?hl=nl&source=iglk [Referral]
http://www.google.com/reader/view/?tab=my [Referral]
http://stackoverflow.com/questions/1567112/convert-keith-hil... [Referral]
http://www.twittergadget.com/gadget.asp?lang=en&country=us&.... [Referral]
PowerGUI OR PowerShell OR DSotnikov OR "Quest Software" (search.twitter.com) [Referral]
http://longurl.org/ [Referral]
http://twitter.com/ [Referral]
http://twitturls.com/ [Referral]
http://powertwitter.me/ [Referral]
http://www.google.com/reader/view/user/-/state/com.google/re... [Referral]
setting wcf security mode from powershell (www.google.co.in) [Referral]
http://www.google.com/ig?hl=en&source=iglk [Referral]
powershell 2.0 threading (www.google.de) [Referral]
powershell add to clipboard (www.google.com) [Referral]
http://www.google.nl/ig?hl=nl&t=1&refresh=1 [Referral]
http://stackoverflow.com/questions/1567112/convert-keith-hil... [Referral]
powershell (technorati.com) [Referral]
http://my.yahoo.com/ [Referral]
powershell sta (www.google.com) [Referral]
http://pipes.yahoo.com/pipes/pipe.info?_id=346c8298b12253787... [Referral]
forms powershell STA (www.google.de) [Referral]
powershell+STA mode (www.google.com) [Referral]
powershell clipboard gettext (www.google.co.uk) [Referral]
http://www.google.nl/ig?hl=nl&source=iglk (www.google.de) [Referral]
how to set the main function to STA mode in windows application (www.google.co.in) [Referral]
windows forms powershell (www.google.pt) [Referral]
powershell read clipboard (www.google.kz) [Referral]
GetText() + powershell (www.google.co.in) [Referral]
STA PowerShell (www.google.com) [Referral]
path to Powershell 2.0 (www.google.ca) [Referral]
powershell "&" line length limit (www.google.com) [Referral]
Powershell Get text command window (www.google.com) [Referral]
powershell command "line length" (www.google.com) [Referral]
powershell 2.0 clipboard (www.google.com) [Referral]
powershell add-type (www.google.com) [Referral]
powershell 2.0 window.form files (www.google.it) [Referral]
powershell read text from windows clipboard (www.google.com) [Referral]
powershell 2.0 sta mode (www.google.is) [Referral]
get-children mode a powershell (www.google.co.uk) [Referral]
powershell mode get children (www.google.ca) [Referral]
System.Windows.Forms powershell (www.google.ca) [Referral]
powershell 2.0 sta (www.google.co.uk) [Referral]
powershell system clipboard (www.google.com) [Referral]
System.Windows.Clipboard.Contains Text() (www.google.co.ve) [Referral]
powershell 2.0 sta (www.google.com) [Referral]
console "powershell 2.0" (www.google.fr) [Referral]
powershell file to clipboard (www.google.de) [Referral]
http://stackoverflow.com/questions/1567112 [Referral]
Powershell STA (www.google.de) [Referral]
http://www.powergui.org/thread.jspa?threadID=9403 [Referral]
powershell command length (www.google.com) [Referral]
powershell clipboard sta (www.google.de) [Referral]
powershell mta mode (www.bing.com) [Referral]
http://ig.gmodules.com/gadgets/ifr?view=home&url=http://1o4.... [Referral]
powershell 2.0 forms (www.google.com) [Referral]
powershell sta mode (www.google.com) [Referral]
Powershell mode STA (www.google.be) [Referral]
powershell clipboard windows.forms.clipboard]::GetText() (www.google.cn) [Referral]
powershell sta sharepoint (www.google.com) [Referral]
powershell 2.0 clipboard (www.google.com.au) [Referral]
powershell 2.0 Windows Form (www.google.se) [Referral]
stawww.google.com/ (www.google.com) [Referral]
http://wiki.openqa.org/display/~jimjosse [Referral]
powershell 2.0 clipboard (www.google.de) [Referral]
powershell "add-type" "Windows.forms" (www.google.de) [Referral]
http://aimtrust.com/aim/r/60008 [Referral]
three view powershell windows forms (www.google.com) [Referral]
powershell system.windows.clipboard (www.google.ru) [Referral]
"powershell 2.0" (hta OR "windows forms") (www.google.com) [Referral]
powershell systems.forms thread (www.google.com.jm) [Referral]
http://uiyxmempivyq.freehostia.com/ [Referral]
powershell sta (www.google.be) [Referral]
powershell format linelength (www.google.fi) [Referral]
powershell mta mode (www.bing.com) [Referral]
powershell clipboard (www.bing.com) [Referral]
$text = & {powershell –sta {add-type –a system.windows.forms; [windows.forms.clipboard]::GetText()}} (www.google.de) [Referral]
powershell sta (www.google.com) [Referral]
powershell 2.0 threading (www.google.com) [Referral]
powershell "from the clipboard" (www.google.com) [Referral]
+powershell +"System.Windows.Forms" (www.google.de) [Referral]
powershell system.windows.clipboard (www.google.com) [Referral]
powershell mta mode (www.google.com) [Referral]
powershell sharepoint test-path (www.google.com) [Referral]
powershell clipboard (www.bing.com) [Referral]
http://powergui.org/thread.jspa?threadID=9403&start=0&tstart... [Referral]
powershell 2.0 threading (www.google.com.au) [Referral]
how to tell if powershell is in mta or sta mode (www.bing.com) [Referral]
powershell text (www.google.com) [Referral]
System.Windows.Forms get-content get-children powershell (www.google.ca) [Referral]
how to run powergui in sta mode (search.yahoo.com) [Referral]
http://sn133w.snt133.mail.live.com/mail/TodayLight.aspx?layo... [Referral]
powershell 2.0 forms (www.google.nl) [Referral]
powershell 2.0 threading (www.google.com) [Referral]
powershell clipboard (search.yahoo.com) [Referral]
create forms powershell 2.0 (www.google.ch) [Referral]
PowerShell Length of text (www.google.ru) [Referral]
powershell put text on clipboard (www.google.com) [Referral]
PowerShell Command line length limit (www.google.ca) [Referral]
add to clipboard with powershell (www.google.com) [Referral]
http://yandex.ru/yandsearch?text=System.Windows.Forms+powers... [Referral]
powershell clipboard /sta /mta (www.google.cz) [Referral]
powershell threading (www.google.pl) [Referral]
sn133w.snt133 mean (www.bing.com) [Referral]
windows powershell form text limit (www.bing.com) [Referral]
powershell clipboard 2.0 (www.google.ca) [Referral]
powershell sta mode (www.google.com.tw) [Referral]
stathread powershell (www.google.de) [Referral]
powershell commandline limit (www.google.com.my) [Referral]
"command line length" powershell (www.google.com) [Referral]
powershell windows.forms.clipboard (www.google.com) [Referral]
PowerShell set STA xml (www.google.com) [Referral]
"virtual XP Mode on Windows 7" (www.google.de) [Referral]
powershell 2.0 remove explorer temp (www.google.co.kr) [Referral]
powershell sta (www.google.de) [Referral]
powershell sta (www.google.com) [Referral]
powershell 2.0 write text to a file (www.google.com) [Referral]
powershell "add-type" linq (www.google.com) [Referral]
powershell clipboard settext (www.bing.com) [Referral]
powershell sta mode (www.google.de) [Referral]
get values powershell form (www.google.com) [Referral]
"powershell -sta" (www.google.com) [Referral]
powershell 2.0 threading (www.google.de) [Referral]
powershell read from clipboard (www.google.pl) [Referral]
function Get-ClipBoard { Add-Type -AssemblyName System.Windows.Forms (www.google.pl) [Referral]
powershell clipboard file (www.google.com) [Referral]
powershell function sta mode (www.google.com.au) [Referral]
http://blogs.technet.com/technetczsk/pages/serial-windows-po... [Referral]
http://www.google.it/imgres?imgurl=http://www.nivot.org/imag... [Referral]
http://theinvestblog.com/ [Referral]
powershell Get-content and get-child (www.google.com) [Referral]
powershell command line length limit (www.google.de) [Referral]
clipboard powershell sta (www.google.com) [Referral]
PowerShell clipboard (www.google.ru) [Referral]
powershell "windows.forms.clipboard" (www.google.com) [Referral]
powershell 2.0 forms (www.google.co.uk) [Referral]
command to add to system clipboard (www.bing.com) [Referral]
Read text powershell (www.google.com) [Referral]
powershell text add (www.google.nl) [Referral]
sn133w.snt133.live.mail.com (www.google.it) [Referral]
sn133w.snt133.mail.live.com (www.google.it) [Referral]
get-content "line length" (www.bing.com) [Referral]
powershell sta (www.google.com.au) [Referral]
using powershell 2.0 hta (www.google.be) [Referral]
powershell command line clipboard (www.google.co.uk) [Referral]
powershell clipboard (www.bing.com) [Referral]
http://www.raypharma.com/acheter-suprax-france.html [Referral]
sn133w.snt133.mail.live.com (www.google.it) [Referral]
powershell 2.0 main function (www.google.com) [Referral]
http://www.raypharma.com/acheter-triderm-france.html [Referral]
powershell 2.0 form (www.google.fr) [Referral]
Clipboard.SetText powershell (www.bing.com) [Referral]
PowerShell get clipboard text (www.google.cn) [Referral]
+powershell xp command line limit (www.google.com) [Referral]
powershell yahoo mail (www.google.com) [Referral]
http://sn133w.snt133.mail.live.com (www.google.it) [Referral]
powershell 2.0 threading (www.google.com) [Referral]
powershell clipboard line break (www.google.de) [Referral]
powershell sta (www.google.com) [Referral]
powershell 2.0 clipboard (www.google.no) [Referral]
http://www.leporelo.eu/blog.aspx?id=how-powershell-can-help-... [Referral]
Comments are closed.