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

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

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.

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish