We help IT Professionals succeed at work.

Powershell: Extra blank lines in pipeline to CLIP command?

Bill Prew
Bill Prew asked
on
High Priority
33 Views
Last Modified: 2020-03-09
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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
The culprit is the "Select-String".
Select-String returns a MatchInfo object, which will be displayed with blank lines when used as string.
The actual string that matched is in the property "Line".
You can extract it from there as string using Select-Object with "-ExpandProperty" (which can be combined with the "-First 1").
$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 -ExpandProperty Line -First 1 |
	& clip.exe

Open in new window

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Not when using clip.exe; clip.exe adds a final CRLF when it puts a string to the clipboard.
You can use Set-Clipboard instead, but that requires PS 5.x.
Jason CrawfordTransport Ninja
CERTIFIED EXPERT

Commented:
What are you planning on doing with the data stored in the clipboard?
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Author

Commented:
Thanks for the guidance.

»bp
Jason CrawfordTransport Ninja
CERTIFIED EXPERT

Commented:
Ok then

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Author

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

»bp