# list of computers to connect to $listOfComputers = 'PC10','TRAIN1','TRAIN2','AD001' # exclude your own computer $listOfComputers = $listOfComputers-ne$env:COMPUTERNAME # code to execute remotely $code = { "Hello" | Out-File-FilePath"c:\users\Public\Desktop\result.txt" } # invoke code on all machines Invoke-Command-ScriptBlock$code-ComputerName$listOfComputers-Throttle1000
# "ping" for PowerShell remoting # tests anonymously whether you can reach the target $IPorNameTargetComputer = 'place name or IP address here' Test-WSMan$IPorNameTargetComputer
# do not store passwords in production solutions, # or you MUST control access permissions to this sensitive data $username = "mydomain\UserName" $password = 'Password' | ConvertTo-SecureString-AsPlainText-Force $domainName = 'NameOfDomain'
# convert username and password to a credential $cred = [PSCredential]::new($username, $password) # join computer Add-Computer-DomainName$domainName-Credential$cred # restart computer Restart-Computer