Skip to content

How to Run a PowerShell Command on Multiple Windows Servers Print

  • 0

How to Run a PowerShell Command on Multiple Windows Servers

Applies to: Windows Server 2019, 2022 and 2025.

PowerShell Remoting can execute the same read or change operation across a controlled server list. Limit concurrency and isolate failures.

Automation magnifies both correct and incorrect actions. Use source control, peer review, test environments and least privilege. Visit networkmanager.info.

Prerequisites

  • Enable secured WinRM.
  • Use DNS names and Kerberos.
  • Prepare an approved target list.
  • Begin with read-only commands.

Procedure

Step 1: Define targets

Use an explicit list or CMDB export.

$servers='SRV-APP01','SRV-APP02','SRV-FILE01'

Step 2: Test connectivity

Identify unavailable endpoints.

$servers | ForEach-Object { Test-WSMan $_ -ErrorAction SilentlyContinue }

Step 3: Run inventory

Collect version without changing servers.

Invoke-Command -ComputerName $servers -ScriptBlock { Get-ComputerInfo | Select CsName,WindowsProductName,OsBuildNumber }

Step 4: Throttle changes

Use Invoke-Command -ThrottleLimit with a reviewed idempotent script.

Step 5: Capture results

Export objects and errors to protected logs.

Verification

Compare returned hostnames with the target list and investigate missing servers.

$results=Invoke-Command -ComputerName $servers -ScriptBlock { hostname; Get-Date } -ErrorVariable RemoteErrors
$results
$RemoteErrors

Rollback

Inventory is read-only. Every change script must include its own tested rollback and per-server result handling.

Security notes

Never build a server list from untrusted input or run broad destructive commands without WhatIf and review.

Official references

Explore Netcloud24 Canada.


Was this answer helpful?

« Back