Output string value from Get-ChildItem - powershell

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.

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

Can't remove all the other files but those in one variable in powershell

I have a script that filters my logs, but the problem is that when I would like to delete everything else but certain files I get errors of Unrecognized escape sequence. I've been trying to split the values but it seems that nothing works. I also tried -exclude before, but didn't get it to work. It's supposed to remove all the other files but $result and $clr.
$files = #()
[xml]$photonconfig = Get-Content C:\Users\Administrator\Desktop\PhotonServer.config
$photonconfig.SelectNodes("Configuration/*") | Select-Object -Expand Name | % {
$_.Replace("xxx","")
} | ForEach {
$files+= Get-ChildItem C:\Users\Administrator\Desktop\log\log/*$_*.log |
sort -Property LastWriteTime -Descending |
Select-Object -First 3
}
$result = $files | Sort-Object LastAccessTime -Descending |
Select-Object -First 3
$clr = "PhotonCLR.log"
$all = Get-ChildItem C:\Users\Administrator\Desktop\log\log/* |
Where-Object { $_.Name -notmatch $result } |
Remove-Item
The second operand of the -match and -notmatch operators is a regular expression, not an array of file names. Use the -contains operator instead:
... | Where-Object { $result -notcontains $_.Name } | ...
On PowerShell v3 and newer you can also use the -notin operator, which feels a little more "natural" to most people:
... | Where-Object { $_.Name -notin $result } | ...
Note that for this to work you also need to expand the Name property when building $result:
$result = $files | Sort-Object LastAccessTime -Descending |
Select-Object -First 3 -Expand Name

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

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