Link to home
Create AccountLog in
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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Bill Prew
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
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
What are you planning on doing with the data stored in the clipboard?
Thanks for the guidance.

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

»bp