Here’s a few PowerShell scripts I use at work for interacting with multiple, networked computers.



Copy file

The $Computers parameter should be a single column of hostnames or IP addresses while $File should be an absolute path to your file. Anyway, as an example use-case, I originally wrote this script to place a link to our organization’s web portal.

Param(
    [string]$Computers,
    [string]$File
)

$Destination = '\\$_\C$\Users\Public\Desktop'

Get-content $Computers |
ForEach-Object {
	$Computer = $_
	If (Test-Connection -Count 2 -ComputerName $Computer -Quiet) {
		Write-Output "[+] $Computer is online."
		Try {
			ForEach-Object {
				Write-Output "[+] Copying $File to $Computer.`n"
				Copy-Item -Path $file -Destination $Destination
			}
		} Catch { "[x] Failed to copy $File to $Computer.`n" }
	} Else { Write-Output "[x] $Computer appears to be offline.`n" }
}

To run, adjust your Execution Policy and ensure ComputerList.txt is in your current working directory.

Set-ExecutionPolicy Unrestricted
./CopyFile.ps1 -Computers ./ComputerList.txt -File ./LinkToPortal.lnk


Schedule a job

Using this script will create a Scheduled Task on a networked computer. Keep in mind the task will run as SYSTEM.

code goes here

To run, adjust your Execution Policy and ensure ComputerList.txt is in your current working directory.

Set-ExecutionPolicy Unrestricted
./ScheduleJob.ps1 -Computers ./ComputerList.txt


Get info

This script will use WMI to get the Serial Number and Current/Last User of multiple, networked computers. If you run into any issues, check the format of your ComputerList.txt file (it will need hostnames until I update this script).

Param([string]$Computers)

Get-Content $Computers |
ForEach-Object {
	$Computer = $_
	Write-Output "---------------------------------------"
	If (Test-Connection -ComputerName $Computer -Count 2 -Quiet) {
		$Serial = Invoke-Command -ComputerName $Computer -ScriptBlock {
			Get-WmiObject -Class Win32_BIOS | Select -ExpandProperty SerialNumber
			}
		$CurrentUser = Invoke-Command -ComputerName $Computer -ScriptBlock {
			Get-WmiObject -Class Win32_ComputerSystem | Select -ExpandProperty UserName
			}
		Write-Output "[+] $Computer | $Serial | $CurrentUser"
	} Else { Write-Output "[x] Failed to ping $Computer." }
} | Tee -Append "./ComputerInfo_$(Get-Date -Format yyyymmdd_HHMMss)"

To run, adjust your Execution Policy and ensure ComputerList.txt is in your current working directory.

Set-ExecutionPolicy Unrestricted
./GetInfo.ps1 -Computers ./ComputerList.txt