I know practically nothing about PowerShell but need to do a very specific task: develop a one-liner to write the contents of a RTF file to the Windows clipboard.
I have separate pieces that work, but lack the syntax knowledge to hook it all together.
This little script works, if saved to blah.ps1 then invoked as ./blah.ps1, but I need to avoid using a script.
$data = New-Object System.Windows.Forms.DataObject
$rtf = Get-Content -Path foo.rtf -Raw
$data.SetData([System.Windows.Forms.DataFormats]::Rtf, $rtf)
[System.Windows.Forms.Clipboard]::SetDataObject($data)
But I need to express it as a one-liner more like this:
Get-Content foo.rtf | $data = New-Object System.Windows.Forms.DataObject; $data.SetData([System.Windows.Forms.DataFormats]::Rtf, $_); [System.Windows.Forms.Clipboard]::SetDataObject($data)
which fails with
At line:1 char:24
+ Get-Content foo.rtf | $data = New-Object System.Windows.Forms.DataOb ...
+ ~~~~~
Expressions are only allowed as the first element of a pipeline.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline