Shorter way to select latest package path using Powershell - 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

Related

How to get five files with most lines in the current directory by simplest way?

There is such a shell command in the chapter "transformational programming" of "The Pragmatic Programmer".
Its function is to list the five files with the most lines in the current directory.
$ find . -type f | xargs wc -l | sort -n | tail -6 | head -5
470 ./debug.pml
470 ./test_to_build.pml
487 ./dbc.pml
719 ./domain_languages.pml
727 ./dry.pml
I'm trying to do the same thing with PowerShell,But it seems too wordy
(Get-ChildItem .\ | ForEach-Object {$_ | Select-Object -Property 'Name', #{label = 'Lines'; expression = {($_ | Get-Content).Length}}} |Sort-Object -Property 'Lines')|Select-Object -Last 5
I believe there will be a simpler way, but I can't think of it.
How to get files with most lines in the current directory by simplest way using PowerShell?
Of course, you don't need to use custom aliases and abbreviations to shorten the length. Although it looks more concise, it loses readability.
Get-Content * | Group-Object PSChildName | Select-Object Count, Name |
Sort-Object Count | Select-Object -Last 5
I finally found my own satisfactory answer!
Used 3 pipeline operators, shell used 5!
What's more, what we get is the object, which can be used for more extensible operations.
I feel better than shell of linux.
dir -file | sort {($_ | gc).Length} | select -l 5
Try either File.ReadLines with Linq or File.ReadAllLines with Count property.
File.ReadLines
Get-ChildItem .\ -File |
Select-Object -Property Name, #{n='Lines'; e= {
[System.Linq.Enumerable]::Count([System.IO.File]::ReadLines($_.FullName))
}
} | Sort-Object -Property 'Lines' -Descending | Select-Object -First 5
File.ReadAllLines
Get-ChildItem .\ -File |
Select-Object -Property Name, #{n='Lines'; e= {
[System.IO.File]::ReadAllLines($_.FullName).Count
}
} | Sort-Object -Property 'Lines' -Descending | Select-Object -First 5
A fast approach would be to use switch -File:
$files = (Get-ChildItem -File ).FullName
$result = foreach ($file in $files) {
$lineCount = 0
switch -File $file {
default { $lineCount++ }
}
[PsCustomObject]#{
File = $file
Lines = $lineCount
}
}
$result | Sort-Object Lines | Select-Object -Last 5

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 find latest file and open in file explorer

This is based on another question: https://stackoverflow.com/a/9675811/14529561.
How would I pass the result from gci path | sort LastWriteTime | select -last 1 and open with explorer.exe?
I have tried:
$wd = gci path | sort LastWriteTime | select -last 1;
explorer $wd
gci path | sort LastWriteTime | select -last 1 | Format-Table -hidetableheaders | explorer $_.
gci path | sort LastWriteTime | select -last 1 | Format-Table -hidetableheaders | ii $_.
Unfortunately, all the above give me give me errors.
You can reference the PSParentPath property.
Get-ChildItem -Path path |
Sort-Object -Property LastWriteTime |
Select-Object -Last 1 | Foreach-Object {
Invoke-Item $_.psparentpath
}
You can do it like this :
$Mypath=$Env:Temp
gci $Mypath | sort LastWriteTime | select -last 1 | % { Explorer /n,/select,$_.FullName }
And if you want to explore in more than folder, you can put thel into an array and do like this :
cls
$Mypath=#("$Env:UserProfile\desktop","$Env:Temp","$Env:AppData")
ForEach ($P in $Mypath) {
gci $P | sort LastWriteTime | select -last 1 | % { Explorer /n,/select,$_.FullName }
}

Powershell select most recent file containing a specific string

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...

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.