find, loop through all windows servers??

Welcome Forums General PowerShell Q&A find, loop through all windows servers??

This topic contains 2 replies, has 2 voices, and was last updated by

5 years, 7 months ago.

  • Author
  • #7381

    Points: -19
    Rank: Member

    Finally!!! After loading this site into several browsers I find one that allows me to TYPE my post!!! 🙂 🙂 🙂

    I don't know where to find/get the old forums since I think I may have asked this already somewhere.

    I want to get and loop through all the servers that PowerShell can find and know about — aka powered-on, check if it's a Windows server like so:

    This is PS 2.0 code, so:

    $servers = $servers | foreach ($_name)

    If Get-ADComputer – filter {operatingsystem -Like "Windows Server*"}

    copy \\$server\c$\*changelog.txt \\foo\backup

    end if

    next end for (I can find whatever it is that ends a for loop)

    That's the general code. Where I'm stuck is:

    1) constructing the for-each loop to get the servers

    2) testing the server

    Do I have these parts correct??

    Thank you, Tom

  • #7385

    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    The old forums content was pulled over, so it's here and searchable.

    PowerShell uses C-like syntax. So you're thinking...

    $computers = Get-ADComputer -filter (whatever)
    foreach ($computer in $computers) {
      if ($this -eq $that) {
        # do whatever

    In terms of testing the server, it depends a bit. If your network is set up to allow a ping, you can run if (test-host $computer -quiet) { #do whatever } to ping the computer.

  • #7386

    Points: -19
    Rank: Member

    Thank you!! I found the archives once, I can find them again...
    That general code snippet should get me started etc.
    Thank you, Tom

The topic ‘find, loop through all windows servers??’ is closed to new replies.