Powershell select most recent file containing a specific string - powershell

I have written a code in powershell that selects the most recent file in a directory.
$first = Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1
$first.name
However, I need to select the most recent file containing a specific string in the name. How can I adapt my code in order to do this?

I got it to work using this:
$filterIRP1064="IRP_1064*"
$latest1064 = Get-ChildItem -Path $dir -Filter $filterIRP1064 | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest1064.name

#Michael Hoffmann
Like this?
$first = Get-ChildItem -recurse | Select-String -pattern "stringhere" | group path | select name
Get-ChildItem -Path $dir | Sort-Object CreationTime -Descending | Select-Object -First 1
$first.name

Get-ChildItem -recurse | Select-String -pattern "stringhere" | group path | select name
Use this to get all the files containing your string. Select the most recent one afterwards.

Get-ChildItem -path $dir | Select-String -pattern "stringhere" | group path | Sort-Object CreationTime -Descending | Select-Object -First 1 | select name
This should work...

Related

Powershell get directory name

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

PowerShell cmdlet to get a name of recently zipped folder?

I have tried
$latest1 = gci $path -Include *.zip| ? { $_.PSIsContainer } | sort CreationTime -desc | select -f 1
but $latest1 is giving blank output.
The -Include switch will only work if the path ends in \* or when used together with the -Recurse switch.
Since in your case, you are only looking for zip files, I would use the -Filter parameter.
$latest1 = Get-ChildItem $path -Filter '*.zip' -File |
Sort-Object CreationTime -Descending | Select-Object -First 1
For PowerShell version below 3.0 use
$latest1 = Get-ChildItem $path -Filter '*.zip' | Where-Object { !$_.PSIsContainer } |
Sort-Object CreationTime -Descending | Select-Object -First 1
If a zip file was found in the path, $latest should now be a FileInfo object with properties like FullName, BaseName etc.
Try changing your where-object filter or removing it
$latest1 = gci $path -Include *.zip| ? { $_.PSIsContainer -eq $false } | sort CreationTime -desc | select -f 1
$latest1 = gci $path -Include *.zip| | sort CreationTime -desc | select -f 1

powershell script statement to fetch a particular file path

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

Shorter way to select latest package path using Powershell

The aim is to select the path to the latest package. Although the code works, there should be a shorter way.
Paths
PS C:\temp> Get-Childitem "C:\google\*\SDK Manager.exe" | % { $_.FullName } | Sort-Object eventid -descending
C:\google\adt-bundle-windows-x86_64-20140702\SDK Manager.exe
C:\google\adt-bundle-windows-x86_64-20130702\SDK Manager.exe
Select latest package path
PS C:\temp> Get-Childitem "C:\google\*\SDK Manager.exe" | % { $_.FullName } | Sort-Object eventid -descending | Select-Object -first 1
C:\google\adt-bundle-windows-x86_64-20140702\SDK Manager.exe
I dont think there is an eventid to sort on Sort-Object eventid -descending? Is that a mistake? Sort-Object wont throw and error if the what you are sorting on does not exist.
Are you looking for brevity?
(Get-ChildItem "C:\google\*\SDK Manager.exe").FullName | Sort-Object -Descending | Select-Object -First 1
which could be shortened using aliases to:
(gci "C:\google\*\SDK Manager.exe").FullName | sort -Descending | Select -first 1

Output string value from Get-ChildItem

I got the following string from Output sub-folder with the latest write access
Get-ChildItem $FilePath | Sort {$_.LastWriteTime} -Descending | where {$_.PsIsContainer} |Select {$_.Name} -First 1
But the output is :
$_.Name
Username
The output I am trying to get is :
Username
I tried formatting the output by :
(Get-ChildItem $FilePath | Sort {$_.LastWriteTime} -Descending | where {$_.PsIsContainer} |Select {$_.Name} -First 1).name
But I'm not sure why it's not working.
Thank you
try this:
Get-ChildItem $filepath | ? { $_.PsIsContainer} | Sort LastWriteTime -Descending | Select -expa Name -First 1
I've anticipaded the where-object, aka '?' , for better performance.
With select-object the {} are needed only for calculated properties and to avoid the coloumn name use the -expand parameter.