Powershell get directory name - powershell

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

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.

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

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.