Avatar of Bill Prew
Bill Prew
 asked on

Powershell: Extra blank lines in pipeline to CLIP command?

I'm trying to get a file path and place it into the clipboard using Powershell.  Below is what I have, and while it likely could be optimized, it seems to be finding a single file path as I desire.  But the output that ends up in the clipboard includes some extra blank lines above and below the actual file path I want.  I've tried a number of ways to try and remove these with no success, so looking for help.

For background I'm trying to query the Windows recently used files and pull out the single most recent one that was for a *.pdf or *.png file.  That logic seems to be working (feel free to suggest improvements), but the problem is the result file path has blank lines above and below in the clipboard after this runs.

I may be mixing up objects and strings (still rooted in a DOS CMD background) or maybe there is something else involved.

$WScript = New-Object -ComObject WScript.Shell; Get-ChildItem -Path "$env:APPDATA\Microsoft\Windows\Recent\*.lnk" | Sort-Object -Property LastWriteTime -Descending | ForEach-Object {$WScript.CreateShortcut($_.FullName).TargetPath} | Select-String -Pattern '\.(pdf|png)$' | Select-Object -First 1 | Clip

Open in new window


»bp
Powershell

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

ASKER
Thanks, any way to prevent the trailing <CR><LF> after the file path, ideally I might want just the path, no new line at the end?


»bp
EXPERT CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jason Crawford

What are you planning on doing with the data stored in the clipboard?
Bill Prew

ASKER
Thanks for the guidance.

»bp
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Jason Crawford

Ok then

Bill Prew

ASKER
What are you planning on doing with the data stored in the clipboard?
It's just being consumed by another Windows application.

»bp