Q. How can I get just the first entry from a list of entries?

If you've saved the objects in a variable, as in these examples, you could also use syntax such as $services[0] to access the first object in the collection.

Don Jones

June 28, 2010

1 Min Read
laptop screen showing a line of black folders with one red folder

For more technical explainers on PowerShell, read our updated 2021 report: PowerShell 101: A Technical Explainer for IT Pros.

Q. How can I get just the first entry from a list of entries?

A. Assuming you're running something like this

$services = Get-Service

the easiest way to get just a certain number of first or last entries is

$service = Get-Service | Select-Object -first 1

There's also the -last parameter that works in a similar way. If you've saved the objects in a variable, as in these examples, you could also use syntax such as $services[0] to access the first object in the collection.

Do you have a Windows PowerShell question? Find more PowerShell FAQs, articles, and other resources at windowsitpro.com/go/DonJonesPowerShell.

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like