Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Get-Clipboard returns $null causing problems with PShell

Aug
2,799
144
This works from PowerShell 5.1

Code:
PS R:\temp> get-content function:tcc

        & "C:\Program Files\JPSoft\TCMD36\tcc.exe" /I /Q /C $args

PS R:\temp>

PS R:\temp> tcc "echo Line1^nLine2^nLine3 > clip:"
PS R:\temp> tcc type clip:
Line1
Line2
Line3
PS R:\temp> .\pscode.ps1
Line1 Line2 Line3
PS R:\temp> type .\pscode.ps1
$continuous = (Get-Clipboard -Raw) -replace "\r?\n", " "
$continuous
PS R:\temp>

This also works from PowerShell 5.1

Code:
PS R:\temp> tcc "echo Line1^nLine2^nLine3 > clip:"
PS R:\temp> tcc type clip:
Line1
Line2
Line3
PS R:\temp> $continuous = (Get-Clipboard -Raw) -replace "\r?\n", " "
PS R:\temp> $continuous
Line1 Line2 Line3
PS R:\temp>

This does not work from TCC;

Code:
ver
echo Line1^nLine2^nLine3 > clip:
echo Contents of Windows Clipboard
type clip:
pshell /s "$continuous = (Get-Clipboard -Raw) -replace "\r?\n", " ""
echo After removal of CRLF from contents of Windows Clipboard
pshell /s "$continuous"

Results from TCC;

Code:
TCC  36.52.88 x64   Windows 10 [Version 10.0.19045.7663]
Contents of Windows Clipboard
Line1
Line2
Line3
After removal of CRLF from contents of Windows Clipboard

R:\>

This does not work from TCC running PSCode.ps1

Code:
R:\>ver

TCC  36.52.88 x64   Windows 10 [Version 10.0.19045.7663]

R:\>type clip:
Line1
Line2
Line3

R:\>type r:\temp\pscode.ps1
$continuous = (Get-Clipboard -Raw) -replace "\r?\n", " "
$continuous

R:\>pshell r:\temp\pscode.ps1


R:\>

I'm thinking that the \r?\n in the PowerShell 5.1 code is not welcomed by TCC.

Joe
 
Last edited:
Well, \r?\n works in another instance.

Ref: PShell and In-Process Pipes

I'm beginning to think that the problem is how TCC gets the clipboard from PowerShell 5.1

Code:
R:\>type clip:
Line1
Line2
Line3

R:\>pshell /s "Get-Clipboard"

R:\>pshell /s "Get-Clipboard -Raw"

R:\>

Yep.

Joe
 
This works.

Code:
R:\>type lines.txt > clip:

R:\>pshell /s "[TakeCommand.PowerShellHost]::InvokeCommand('type clip:')"
Line1
Line2
Line3

R:\>pshell /s "[TakeCommand.PowerShellHost]::InvokeCommand('type clip:') -replace '\r?\n', ' '"
Line1 Line2 Line3

R:\>

Joe
 
A more readable version;

Code:
set GetClipboard=[TakeCommand.PowerShellHost]::InvokeCommand('type clip:')

pshell /s "%GetClipboard -replace '\r?\n', ' '"

Joe
 
Here's how I use it in a .ps1 file,
which can be run either using PShell or from PowerShell 5.1

Code:
$PSPID = Get-Process PowerShell

if ($pid -eq $PSPid.Id)
    {
        $continuous = (Get-Clipboard -Raw) -replace "\r?\n", " "
    }
    else
    {
        $env:GetClipboard=[TakeCommand.PowerShellHost]::InvokeCommand('type clip:')
        $continuous = $env:GetClipboard -replace '\r?\n', ' '
    }

$continuous

From TCC v36
Code:
R:\>pshell pscode.ps1
Line1 Line2 Line3

From PowerShell 5.1
Code:
PS R:\> .\pscode.ps1
Line1 Line2 Line3

Joe
 
Back
Top