# 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 Script Provider
PowerShell ISE Hacking: Change default save encoding to ASCII
PowerShell 2.0 – PSCX Labs: Invoke-Reflector
PowerShell 2.0 – Developer Essentials #1 – Initializing a Runspace with a Module
SharePoint Resources & Localization – What, Where and Why?
PowerShell 2.0 – Partial Application of Functions and Cmdlets

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]
powershell 2.0 text to clipboard (www.google.com) [Referral]
powershell 2.0 path ./ (www.google.com) [Referral]
http://vmtoolkit.com/members/CialisRezeptfrei.aspx [Referral]
http://www.kwick.de/cialisbestellen [Referral]
powershell get-clipboard (www.google.de) [Referral]
powershell clipboard (www.google.co.uk) [Referral]
PowerShell –sta (www.google.com) [Referral]
powershell add-type forms (www.google.ru) [Referral]
powershell 2.0 Reading text files (www.google.com) [Referral]
console.setin powershell (www.google.com) [Referral]
powershell clipboard set text (www.bing.com) [Referral]
powershell 2.0 formular (www.google.at) [Referral]
powershell System.Windows.Forms.Clipboard (www.google.com) [Referral]
[System.Windows.Forms text powershell (www.google.fr) [Referral]
powershell MTA (www.google.com) [Referral]
powershell 2.0 find text in window (www.google.com) [Referral]
google.nl/ig (www.google.co.uk) [Referral]
+"Clipboard.SetText" +thread +STA +"asp.net" (www.google.com.br) [Referral]
powershell sta mode (www.google.co.uk) [Referral]
yahoo mailwww.google.com (www.google.ca) [Referral]
powershell read text file and format on form (www.google.com) [Referral]
powershell set from mta to sta (www.google.at) [Referral]
powershell get-content $args (www.bing.com) [Referral]
http://pravofinansi.ru/ [Referral]
starting powershell 2.0 console (www.google.com) [Referral]
PowerShell Clipboard (www.bing.com) [Referral]
http://www.nivot.org/2009/10/14/PowerShell20GettingAndSettingTextToAndFromTheClipboard.aspx (www.google.com) [Referral]
http://pravotoday.ru/ [Referral]
powershell 2.0 threading (www.google.com) [Referral]
HTA und powershell 2.0 (www.google.de) [Referral]
powershell 2.0 threading (www.google.com) [Referral]
Clipboard.SetText vsto (search.yahoo.com) [Referral]
powershell get-clipboard (www.google.com) [Referral]
938 (www.raypharma.com) [Referral]
powershell 2.0 threading (www.google.com) [Referral]
reading text file line by line using powershell 2.0 (www.google.co.in) [Referral]
GetText in powershell (www.google.co.in) [Referral]
system.windows.forms get-process powershell (www.google.ru) [Referral]
powershell check sta mode (www.google.be) [Referral]
Clipboard.GetText MTA (www.google.com.my) [Referral]
powershell text (www.google.com) [Referral]
powershell read file line by line (www.google.com) [Referral]
http://smartforexsignal.com/ [Referral]
powershell limit length (www.google.com) [Referral]
powershell 2.0 threading (www.google.com) [Referral]
http://wszystkie-projekty.pl/ [Referral]
powershell 2.0 linelength (www.google.at) [Referral]
powershell read clipboard (www.bing.com) [Referral]
http://www.leporelo.eu/blog.aspx?category=PowerShell [Referral]
powershell maximum line length (www.google.com) [Referral]
powershell read file line by line (www.google.co.uk) [Referral]
http://www.baidu.com/s?wd=clipboard&pn=20&usm=2 [Referral]
powershell check STA mode (www.google.pl) [Referral]
powershell 2.0 thread (www.google.com) [Referral]
http://www.baidu.com/s?cl=3&wd=powershell+add%2Dtype [Referral]
powergui 2010 "powershell 2.0" (www.google.com) [Referral]
powershell windows.form.clipboard (www.google.com) [Referral]
PowerShell 2.0 working with text files (www.google.co.il) [Referral]
powershell system.windows.clipboard (www.google.com) [Referral]
powershell hta (www.bing.com) [Referral]
powershell [STAThread] (www.bing.com) [Referral]
get child processes powershell (www.google.com) [Referral]
read from a form using powershell (www.google.com) [Referral]
powershell format text (www.google.de) [Referral]
getting vi mode in powershell (www.bing.com) [Referral]
Add-Type System.Windows.Forms (www.bing.com) [Referral]
powershell sta mode check (www.google.co.uk) [Referral]
���� (www.google.com) [Referral]
powershell how to determine sta mode (www.google.com) [Referral]
powershell get text from command (www.google.hr) [Referral]
"Threading" for PowerShell v2.0 (www.google.com) [Referral]
powershell STA (www.google.com) [Referral]
powershell 2.0 threading (www.google.com) [Referral]
sn133w.snt133.mail.live.com (cc.bingj.com) [Referral]
powershell Windows.Forms.Clipboard (www.google.com) [Referral]
Threading for powershell v2.0 (www.google.com) [Referral]
powershell 2.0 sta mode (www.google.com) [Referral]
powershell hta (www.google.com) [Referral]
Powershell STA mode (www.google.com) [Referral]
sn133w snt133 mail live (www.bing.com) [Referral]
powershell put text in clipboard (www.google.se) [Referral]
http://www.center-prestige.ru/catalogue/detail/?section=36&i... [Referral]
http://smartfxsol.com/ [Referral]
powershell limits "command line" length (www.google.co.uk) [Referral]
sta powershell (www.google.com) [Referral]
powershell 2.0 "read file" (www.google.com) [Referral]
powershell "Read file" (www.google.com) [Referral]
powershell 2.0 accessing a text file (www.google.com.au) [Referral]
powershell setting sta (www.google.de) [Referral]
sharepoint powershell "Add-Type -AssemblyName" (www.google.com) [Referral]
sn133w.snt133.mail.live.com / referral (www.google.co.uk) [Referral]
powershell Add-Type -OutputType WindowsApplication -OutputAssembly (www.google.cz) [Referral]
Write windows Application in PowerShell with Add-Type (www.google.cz) [Referral]
powershell ise "command window" encoding (www.google.co.jp) [Referral]
cache:Sw9y9nUHqNQJ:stackoverflow.com/questions/1567112/convert-keith-hills-powershell-get-clipboard-and-set-clipboard-to-a-psm1-script powershell access clipboard (webcache.googleusercontent.com) [Referral]
powershell sta (www.google.de) [Referral]
http://www.stumbleupon.com/refer.php?url=http%3A//www.nivot.... [Referral]
powershell sta (www.google.si) [Referral]
powershell pssession sta (www.google.si) [Referral]
http://www.stumbleupon.com/refer.php?url=http%3A%2F%2Fwww.ni... [Referral]
http://www.lbonline.net/ [Referral]
STA mode POWERSHELL (www.google.co.uk) [Referral]
powershell command-line length limit (www.google.co.jp) [Referral]
powershell write to clipboard (www.bing.com) [Referral]
http://blogs.technet.com/b/technetczsk/archive/2010/02/15/se... [Referral]
powershell how to see child process (www.google.it) [Referral]
http://www.google.com/ [Referral]
http://web-promotion-services.net/ [Referral]
http://nywellwatch.org/ [Referral]
powergui system.Windows.Forms (hk.search.yahoo.com) [Referral]
powershell v2 windows.forms.clipboard (www.bing.com) [Referral]
powershell threading v2.0 (www.google.com) [Referral]
threading powershell 2.0 (www.google.com) [Referral]
HTTP//SN133W.SNT133 (www.bing.com) [Referral]
powershell 2.0 threading (www.google.com) [Referral]
http://www.stopdigitalamnesia.com/alprazolam-0-25-mg-0-5mg-1... [Referral]
powershell get files from clipboard (www.bing.com) [Referral]
http://www.nivot.org/2009/10/14/PowerShell20GettingAndSettingTextToAndFromTheClipboard.aspx (www.google.com) [Referral]
http://www.nivot.org/CommentView,guid,cf5692fe-f0ef-4dc7-aa9b-1473df68c984.aspx (www.google.com) [Referral]
powershell ise access clipboard (www.bing.com) [Referral]
powershell clipboard (www.bing.com) [Referral]
cache:YDKjUYLe1-oJ:www.nivot.org/2009/10/14/PowerShell20GettingAndSettingTextToAndFromTheClipboard.aspx '+7' 'Dmitrii @mail.kz @mail.ru @list.ru @yandex.ru' '2009' '2010' (webcache.googleusercontent.com) [Referral]
stumbleupon.com/referral/php (www.google.com) [Referral]
http://www.google.com.eg/ [Referral]
Threading for PowerShell 2.0 (www.google.com) [Referral]
powershell add-type system.windows.forms (www.bing.com) [Referral]
powershell [windows.forms.clipboard] settext (www.bing.com) [Referral]
powershell Reading Text Files (www.bing.com) [Referral]
cache:Sw9y9nUHqNQJ:stackoverflow.com/questions/1567112/convert-keith-hills-powershell-get-clipboard-and-set-clipboard-to-a-psm1-script power-Shell Get-Clipboard (webcache.googleusercontent.com) [Referral]
STA powershell (www.google.com) [Referral]
Comments are closed.