I'm trying to figure out how to open the selected object I just selected below.
I understand it might have some issues due to it might not know the file it's opening but even an "open with" option would work.
Basically I want to open the last file I was working with similar to the outlook attach file feature where it shows the last document you were working with.
Get-ChildItem -Path 'C:\Users\replace user name here\AppData\Roaming\Microsoft\Windows\Recent' |
Where-Object { -not $_.PsIsContainer } |
Sort-Object LastWriteTime -Descending |
Select-Object -first 1
Pipe to Invoke-Item :
Get-ChildItem -Path 'C:\Users\replace user name here\AppData\Roaming\Microsoft\Windows\Recent' | Where-Object { -not $_.PsIsContainer } | Sort-Object LastWriteTime -Descending | Select-Object -first 1 | invoke-item
or, wrap it in a scriptblock:
invoke-item {Get-ChildItem -Path 'C:\Users\replace user name here\AppData\Roaming\Microsoft\Windows\Recent' | Where-Object { -not $_.PsIsContainer } | Sort-Object LastWriteTime -Descending | Select-Object -first 1}
The Invoke-Item cmdlet works like double-clicking an item in explorer. That should run the default association for the file.
Related
I want a code snippet to put into my profile that will always select the most recent folder.
(gci C:\Users\$env:username\Documents\releases | ? { $_.PSIsContainer } | sort CreationTime | Select-Object Name)[-1]
This is my output.
Name
----
20201116_124047
I would like the output to be a string that I can place into a variable. I am on Powershell 5.1
20201116_124047
try this :
$myvar=Get-ChildItem "C:\Users\$env:username\Documents\releases" -Directory | sort CreationTime -Descending | Select -ExpandProperty Name -First 1
The property I think you need is LastWriteTime, instead of CreationTime.
Also, unless you are using a PowerShell version below 3.0, you can use the -Directory switch and do not have to use Where-Object { $_.PSIsContainer }
Get-ChildItem -Path "C:\Users\$env:username\Documents\releases" -Directory |
Sort-Object LastWriteTime |
Select-Object -ExpandProperty Name -Last 1
I'm trying to open the most recent picture from all my folders in my onedrive using powershell. I have already gotten to the point where i have a "list" which automatically sorts to newest and only gives me the "first 1" result.
I've tried implementing "Select-Object ().Open" but i don't see where i need to implement it. I can't get it to work.
I've also tried taking the directory and filename in variables to try and paste these together to then use these to open the picture.
This is my command:
Get-ChildItem -Recurse | Where-Object {$_.Name -match "(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge "03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1
I'm expecting that there is a way to do this action and open this file in 1 command.
EDIT: I just discovered "Fullname" in the "Select-Object" function which is my directory and filename togheter. I still don't know how to implement ".Open" with this though.
Try this -
$FirstFile = Get-ChildItem -Recurse | Where-Object {$_.Name -match "(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge "03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1
Start-Process $FirstFile.FullName
You can also put the whole thing into a single line, though breaking the code will provide more readability -
Start-Process (Get-ChildItem -Recurse | Where-Object {$_.Name -match "`
(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge`
"03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1).FullName
I have the following script where I'm trying to delete all the SQL .bak files except for the last two. When I run it it wipes out everything in the folder. Does -Exclude not work with array values?
$excludefile=get-childitem D:\TempDB | sort lastwritetime | select-object -Last 2 | select-object -Property Name | select-object -expandproperty Name
foreach ($element in $excludefile)
{
$element
remove-item -Path D:\TempDB -Exclude ($element) -Force
}
Is this what you're looking for?
Get-ChildItem D:\TempDB |
Sort-Object LastWriteTime -Descending |
Select-Object -Skip 2 |
Remove-Item -WhatIf
Of course, you can remove -WhatIf if this is what you need.
I have script that selects .exe files with the specified name from the local folder and removes all files, except first.
$P variable is defined in param.
$P ="$($env:USERPROFILE)\Desktop\I"
Then I got this error
$C = Get-ChildItem $P -Filter *.exe| Where-Object Name -Like '*r_2-2*' | Sort-Object Name -Descending | Select-Object -ExpandProperty Name -Skip 1 | Remove-Item
Remove-Item : Cannot find path 'D:\FM\r_2-2.exe' because it does not exist.
At line:1 char:251
+ ... Descending | Select-Object -ExpandProperty Name -Skip 1 | Remove-Item
I know about foreach loop but want to use For-EachObject cmdlet instead.
You were quite close, if you want to use ForEach-Object:
Get-ChildItem $P -Filter *.exe | Where-Object Name -Like '*r_2-2*' | Select-Object -Skip 1 | ForEach-Object { remove-item $_.FullName -force }
To skip one first found result just Select-Object -Skip 1 is enough.
Remove-Item -Force also removes hidden and read-only files.
You can make the use of FullName parameter directly in your statement. Try this -
$C = Get-ChildItem $P -Filter *.exe| Where-Object Name -Like '*r_2-2*' | Sort-Object Name -Descending | Select-Object -ExpandProperty FullName -Skip 1
$c | ForEach-Object {Remove-Item -Path $_}
Use -Force parameter if you want to delete the hidden files too.
I have the following statement in PowerShell script that fetches a particular file
$imptext = "E:\imp\old"
$strAttachment = dir $imptext | sort -prop LastWriteTime | Where-Object {$_.name -like "*Imptextfiles*"} | Where-Object {$_.lastwritetime -gt (Get-Date).AddHours(-1)} | select -last 1 | foreach-object -process { $_.FullName }
How can I write to host the path of that file $strAttachment along with the exact file name?
You want to get the directory of the file? Just use the Split-Path cmdlet:
$attachmentDirectory = Split-Path $strAttachment
You could also use Get-DirectoryName:
$attachmentDirectory =[System.IO.Path]::GetDirectoryName($strAttachment)
Also, you can improve your inital script (using -Filter instead of two Where-Object)
$strAttachment = Get-ChildItem $imptext -Filter '*Imptextfiles*' |
Where-Object Lastwritetime -gt (Get-Date).AddHours(-1) |
sort LastWriteTime |
select -last 1 |
select -ExpandProperty FullName