Open the first file in a list of recursed folders - powershell

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

Related

How to open an selected object with powershell

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.

Remove-Item cmdlet causes "Cannot find path" while process of removal of .exe files in local folder

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.

Finding Files in a directory equal to 0 Powershell

I was wondering how I can display a list of empty files in a directory
$test = gci "C:\Users\Freedom\Documents" -Recurse
$test | Where-Object {$_.PsISContainer} | Select-Object FullName | Where-Object {$_.GetFiles() -eq 0}
I Don't understand because when I do get-childitem | get-member I get a list of properties and methods I can use and in the list is getfiles() why can't I use this method why's it giving me an error message?
Method invocation failed because [System.IO.FileInfo] does not contain a method named 'GetFiles'.
I think you want this:
Get-ChildItem | Where-Object { (-not $_.PSIsContainer) -and ($_.Length -eq 0) }
If you have PowerShell 3.0 or later you can use this:
Get-ChildItem -File | Where-Object { $_.Length -eq 0 }
Of course you can add whatever other parameters for Get-ChildItem that you want (-Recurse, etc.).
Wow I had what I wanted mixed up! And I had to add the .count to the getfiles() method
$test | Where-Object {$_.PsISContainer} | Where-Object {$_.GetFiles().Count -eq 0} | Select-Object FullName
try this
Get-ChildItem "c:\temp" -File -Recurse | where Length -eq 0
Use Get-ChildItem and the File flag, -Recurse is needed to get every file in the folder and in the folder below. Then get all the files were Get-Content returns null.
Get-ChildItem $YourPath -Recurse -File | Where-Object {!(Get-Content $_.Fullname)}

How exploit Get-ChildItem result?

I would like list all path to go to folder name ending with "_S" recursively.
I did that:
Get-ChildItem -Recurse | Where-Object {($_.Attributes -match "Directory") -and ($_.Name.EndsWith("_S") -and ($_.PSIsContainer -eq 1))}
But the result isn't an array. How i can to exploit the results ?
My goal is to have something like that:
Myfolder\folder1\folder1_S
Myfolder\folder2_S
Use Select-Object and grab the FullName of the file.
Also, as stated in the comments on the question by #Paul ($_.Attributes -match "Directory") and ($_.PSIsContainer -eq 1) is redundant, might want to remove one of them.
Get-ChildItem -Recurse | Where-Object {($_.Attributes -match "Directory") -and ($_.Name.EndsWith("_S"))} | Select-Object -ExpandProperty FullName
The above can also be refactored in PowerShell 3.0+, to
Get-ChildItem -Recurse -Directory -Filter *_S | Select-Object -ExpandProperty FullName
which would recursively get the path of all directories ending with "_S"

Limiting Powershell Get-ChildItem by File Creation Date Range

I use a Powershell command to generate a CSV report of certain file types. My goal is to find out how many were added during a particular date range. Right now, the script finds everything and I sort by date to find my number. I'd like to modify the command to only return objects within a creation date rage, i.e. if this file was created between 1 March 2013 and 31 March 2013. There's probably a way to limit the command by a date range, likely using Select-Object, I just can't figure it out.
Get-ChildItem 'PATH' -recurse -include #("*.tif*","*.jp2","*.pdf") | Select-Object FullName, CreationTime, #{Name="Mbytes";Expression={$_.Length/1Kb}}, #{Name="Age";Expression={(((Get-Date) - $_.CreationTime).Days)}} | Export-Csv 'PATH\scans.csv'
Use Where-Object and test the $_.CreationTime:
Get-ChildItem 'PATH' -recurse -include #("*.tif*","*.jp2","*.pdf") |
Where-Object { $_.CreationTime -ge "03/01/2013" -and $_.CreationTime -le "03/31/2013" }
Use Where-Object, like:
Get-ChildItem 'PATH' -recurse -include #("*.tif*","*.jp2","*.pdf") |
Where-Object { $_.CreationTime -gt "03/01/2013" -and $_.CreationTime -lt "03/31/2013" }
Select-Object FullName, CreationTime, #{Name="Mbytes";Expression={$_.Length/1Kb}}, #{Name="Age";Expression={(((Get-Date) - $_.CreationTime).Days)}} |
Export-Csv 'PATH\scans.csv'
Fixed it...
Get-ChildItem C:\Windows\ -recurse -include #("*.txt*","*.pdf") |
Where-Object {$_.CreationTime -gt "01/01/2013" -and $_.CreationTime -lt "12/02/2014"} |
Select-Object FullName, CreationTime, #{Name="Mbytes";Expression={$_.Length/1Kb}}, #{Name="Age";Expression={(((Get-Date) - $_.CreationTime).Days)}} |
Export-Csv C:\search_TXT-and-PDF_files_01012013-to-12022014_sort.txt