troubleshooting Question

Powershell: Extra blank lines in pipeline to CLIP command?

Avatar of Bill Prew
Bill Prew asked on
Powershell
7 Comments2 Solutions62 ViewsLast Modified:
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

»bp
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros