# Loop for Hostnames

**URL:** <https://forums.powershell.org/t/loop-for-hostnames/20878>\
**Category:** PowerShell Help\
**Created:** [November 9, 2022, 3:55pm UTC](https://forums.powershell.org/t/loop-for-hostnames/20878 "2022-11-09T15:55:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![itlnau](https://avatars.discourse-cdn.com/v4/letter/i/35a633/32.png) [@itlnau](https://forums.powershell.org/u/itlnau)\
**Post date:** [November 9, 2022, 3:55pm UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/1 "2022-11-09T15:55:35Z")

</div>

Hi

I’m new 😇

I’ve found this pretty usefully PowerShell script for checking users in local Admin group.

> invoke-command {  
> $members = net localgroup Administrators | where {$\_ -AND $\_ -notmatch “command completed successfully”} | select -skip 4  
> New-Object PSObject -Property @{  
> Computername = $env:COMPUTERNAME  
> Group = “Administrators”  
> Members=$members  
> }  
> } -computer Host-001,Host-002,Host-003 -HideComputerName | Select \* -ExcludeProperty RunspaceID | Export-CSV c:\Install\local\_admins.csv -NoTypeInformation

Now i’d like to add a loop for Hostname 001 to 100. Hosts which don’t respond should generate a error in the output file whitout stopping the execution.

Any help?

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 9, 2022, 4:46pm UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/2 "2022-11-09T16:46:59Z")

</div>

Hi, welcome to the forum. What exactly would you like help with?

---

<div class="post-metadata">

**Author:** ![alex-innes](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/alex-innes/32/129_2.png) [@alex-innes](https://forums.powershell.org/u/alex-innes)\
**Post date:** [November 10, 2022, 1:19pm UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/3 "2022-11-10T13:19:32Z")

</div>

Hi Itlnau,

You would need to put your invoke command inside a loop for each of the hostnames you have soo…

```auto
$hostnames = @(1..100)
foreach($h in $hostnames){

  Invoke-command {
  ...... } -computer "Host-$h"

}

```

Your only issue with that if your hosts are labelled “Host-001” you will need to do some string manipulation to have the number come through as “001”.

---

<div class="post-metadata">

**Author:** ![itlnau](https://avatars.discourse-cdn.com/v4/letter/i/35a633/32.png) [@itlnau](https://forums.powershell.org/u/itlnau)\
**Post date:** [November 14, 2022, 6:50am UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/4 "2022-11-14T06:50:21Z")

</div>

Thats exactly the case. Any suggestions how to solve this?

---

<div class="post-metadata">

**Author:** ![itlnau](https://avatars.discourse-cdn.com/v4/letter/i/35a633/32.png) [@itlnau](https://forums.powershell.org/u/itlnau)\
**Post date:** [November 14, 2022, 6:53am UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/5 "2022-11-14T06:53:07Z")

</div>

As described:

- Above script with loop from 001 to 100
- and skip not reachable hosts

Thx 🙂

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [November 14, 2022, 8:50am UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/6 "2022-11-14T08:50:55Z")

</div>

You’ll need to explain the problem you’re having. Is it generating a list of hosts with 001-100? Is it writing an error?

---

<div class="post-metadata">

**Author:** ![itlnau](https://avatars.discourse-cdn.com/v4/letter/i/35a633/32.png) [@itlnau](https://forums.powershell.org/u/itlnau)\
**Post date:** [November 14, 2022, 10:56am UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/7 "2022-11-14T10:56:05Z")

</div>

> [@alex-innes](#):
>
> command inside a loop

I’m not sure where it gets unclear, but:

1. PowerShell Script for
2. checking all PC’s within a rang of Hostname001 to Hostname100
3. whom is within the local Administrators group
4. the output is a CSV list with all hostnames (hostname & groups and user within local administrators)
5. if the client is offline or the access is prohibited note an error in CSV file und move to next one.

Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![alex-innes](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/alex-innes/32/129_2.png) [@alex-innes](https://forums.powershell.org/u/alex-innes)\
**Post date:** [November 14, 2022, 11:55am UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/8 "2022-11-14T11:55:05Z")

</div>

There are some simple methods you could use to solve these. For testing to see if a host is online, you can use the command [Test-Connection](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-connection?view=powershell-7.3)

Just to make this clear, this forum is not about us creating a script for you, we are here to help with problems in your own scripts.

As Krzydoug mentioned, what problems are you having? What have you tried to generate the hostnames?

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [November 14, 2022, 11:14pm UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/9 "2022-11-14T23:14:24Z")

</div>

If you are in a domain environment, I would suggest getting the list of computers from AD. Once you have the list, you will need to test connectivity in your loop as their are almost always stale entries.

Another suggestion. If you don’t want to use/mess with remote powershell, you can still use ADSI or WMI to grab the local administrators group info. If you do go with Invoke-Command, I would add Test-WSMan to your loop for each host before executing. In large domain environments, you will surely run into systems with issues.

Also, use try catch blocks to keep your script moving along when issues are found.

One might also suggest you do a quick google search 🙂

> **[How to get the local Administrators group members using PowerShell?](https://www.tutorialspoint.com/how-to-get-the-local-administrators-group-members-using-powershell)**
>
> How to get the local Administrators group members using PowerShell? - To get the local Administrators group members using PowerShell, you need to use the GetLoc ...

Using WMI:

> **[List all users in local Administrators Group - Jean Snyman](https://www.jeansnyman.com/posts/list-all-users-in-local-administrators-group/)**
>
> Jean Snyman - Software Engineer

---

<div class="post-metadata">

**Author:** ![metablaster](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/metablaster/32/1224_2.png) [@metablaster](https://forums.powershell.org/u/metablaster)\
**Post date:** [November 15, 2022, 8:36am UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/10 "2022-11-15T08:36:00Z")

</div>

invoke-command uses PS remoting and WinRM for connection, therefore to test connection to remote host you would do it like this:

follow up on alex-innes sample code…

```powershell
$hostnames = @(1..100)

foreach($h in $hostnames){

  $Cred = Get-Credential -Message "Credentials are required to access Host-$h"
  $TestResult = Test-WSMan -Credential $Cred -ComputerName "Host-$h" -Authentication Negotiate -Port 5985 |
  Select-Object ProductVendor, ProductVersion | Format-List

  # Show test status for debugging
  $TestResult

  if ($TestResult)
  {
      Invoke-command {
           # remote command
  } -computer "Host-$h" -Credential $Cred
}

```

This should work if remote host is HTTP enabled which is default, for HTTPS remoting you would add `-UseSsl` and `-Port 5986` to `Test-WSMan` command and you would also require SSL certificate installed in trusted root of the remote host.

---

<div class="post-metadata">

**Author:** ![itlnau](https://avatars.discourse-cdn.com/v4/letter/i/35a633/32.png) [@itlnau](https://forums.powershell.org/u/itlnau)\
**Post date:** [November 15, 2022, 1:48pm UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/11 "2022-11-15T13:48:46Z")

</div>

Thank you for the response!

It doesn’t solve the counting problem described by @alex-innes →

> Your only issue with that if your hosts are labelled “Host-001” you will need to do some string manipulation to have the number come through as “001”.

Furthermore, there is usually no problem with remote execution, because WinRM works just fine.

My second problem with your skript example is that in the output CSV file gets always overwriten by the last output. This means the CSV file contains only the last host Host-100.

Thank you

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [November 15, 2022, 4:55pm UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/12 "2022-11-15T16:55:27Z")

</div>

> [@itlnau](#):
>
> Your only issue with that if your hosts are labelled “Host-001” you will need to do some string manipulation to have the number come through as “001”.

Is there not a better way to get a list of hosts? Cant you grab them from AD?

---

<div class="post-metadata">

**Author:** ![alex-innes](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/alex-innes/32/129_2.png) [@alex-innes](https://forums.powershell.org/u/alex-innes)\
**Post date:** [November 17, 2022, 12:58pm UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/13 "2022-11-17T12:58:43Z")

</div>

For your hostname problem, I agree with tonyd there must be a better way to get these.

You could write some complicated code to insert the “00” if there is only a single length integer and a “0” for a double length integer, or you could create a CSV with 001…100 and then pull from that.

For your overwriting issue, I would suggest you read the documentation for the command.  
[Export-csv](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-7.3) as there is a parameter that will append to a file rather than overwrite.

---

<div class="post-metadata">

**Author:** ![posh\_enjoyer](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@posh\_enjoyer](https://forums.powershell.org/u/posh_enjoyer)\
**Post date:** [November 22, 2022, 3:18pm UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/14 "2022-11-22T15:18:18Z")

</div>

Agree that Get-ADComputer with some filters is a good way to gather a list to action on.  
Aside from that, to make sure the host numbers are strings you could cast them as [string] inside the loop?  
I could not figure out how do cast inline so made separate variable but there’s probably a better method.

```auto
$hostnames = @(1..100)

foreach($h in $hostnames){

   $hostStr = [string]$h

   ... (Creds and testing) ...

   Invoke-Command -Computer "Host-$hostStr"

}

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/loop-for-hostnames/20878/15 "2024-05-16T20:16:20Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


