Reputation: 13
Here is the Powershell script that I have created so far:
dir C:\Windows\System32 | Sort-Object -Descending
My end goal is to list all the files (files only - no sub directories) in the following directory, sorted by filename in descending order. Next I need to read this directory listing created into Powershell, then display every other filename to the screen.
I was trying to find a way to do this by piping the sorted results into the Foreach-Object cmdlet, and then reading "Object" from the resulting list, using the Get-Content cmdlet, and the FullName proprety of each object - like: Get-Content $_.FullName
dir C:\Windows\System32 | Sort-Object -Descending | ForEach-Object
Any ideas how to make this happen with those cmdlets?
Upvotes: 0
Views: 523
Reputation: 7489
you can get the index number of an item in an array with .IndexOf()
. you can get the even numbers of a list of numbers by using "mod 2 = zero". something like this ...
$TargetDir = "$env:windir\system32"
$FileList = Get-ChildItem -LiteralPath $TargetDir -File |
Sort-Object -Property Name
foreach ($FL_Item in $FileList)
{
# if "number mod 2 = zero", then it's an even number
if ($FileList.IndexOf($FL_Item) % 2 -eq 0)
{
$FL_Item.Name
}
}
Upvotes: 0