# PSSession - many computers

**URL:** <https://forums.powershell.org/t/pssession-many-computers/4354>\
**Category:** PowerShell Help\
**Created:** [June 12, 2015, 7:13am UTC](https://forums.powershell.org/t/pssession-many-computers/4354 "2015-06-12T07:13:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jas\_wheel](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jas\_wheel](https://forums.powershell.org/u/jas_wheel)\
**Post date:** [June 12, 2015, 7:13am UTC](https://forums.powershell.org/t/pssession-many-computers/4354/1 "2015-06-12T07:13:12Z")

</div>

I have two scripts that I created, first one tests to make sure the computers are online (from an AD OU) and IF the computer is online, it writes the computer name to a txt file. The second part of the script takes that txt file and I’m attempting to run a script remotely on all the computers, over 400 of them in all.

So my question is in regard to PSSESSION and how to do this on all computers. I know there is a threshold of 32 PSSESSIONs, so how do I get the script to execure on the other hundreds of computers?

First part of my script: gathers the computers that are in an OU, currently ONLINE and writes them to a text file

* * *

$computers = get-adcomputer -Filter {operatingSystem -eq “Windows Embedded Standard”} -SearchBase “OU=OU, DC=DC, DC=LOC”  
foreach ($computer in $computers) {  
$client = $computer.name

```
if (Test-Connection -ComputerName $client -BufferSize 16 -Count 1 -Quiet)
    {
    Write-output $client | out-file c:\temp\online.txt -append
    }
else
    {
    Write-Warning "$client is offline" 3&gt;&gt; c:\temp\offline.txt
    }
}

```

The second part of the script - all I want to do is remove-item (folders and files) and then close the pssession, move onto the next computers

* * *

$pssession = new-pssession -ComputerName (get-content c:\temp\online.txt)  
invoke-command -session $pssession -ScriptBlock {  
$path1 = “c:\temp\*”  
$path2 = “c:\windows\temp\*”  
$path3 = “c:\support\*”  
$path4 = “c:\windows\system32\ccm\cache\chd00329.8.system”  
$path5 = “c:\windows\system32\ccm\cache\chd00329.9.system”

```
if (test-path -path $path1) {
    remove-item -path $path1 -Recurse
    }
if (test-path -path $path2) {
    remove-item -path $path2 -Recurse
    }
if (test-path -path $path3) {
    remove-item -path $path3 -Recurse
    }
if (test-path -path $path4) {
    remove-item -path $path4 -Recurse
    }
if (test-path -path $path5) {
    remove-item -path $path5 -Recurse
    }
}

```

This seems to work, on 32 computers, but it never goes on to the rest of the computers so I know I’m doing something wrong.

Any help is greatly appreciated

Thanks  
j

---

<div class="post-metadata">

**Author:** ![flynnbundy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/flynnbundy/32/639_2.png) [@flynnbundy](https://forums.powershell.org/u/flynnbundy)\
**Post date:** [June 12, 2015, 5:37pm UTC](https://forums.powershell.org/t/pssession-many-computers/4354/2 "2015-06-12T17:37:59Z")

</div>

Hi There, This is not really a job for a PS-Session. You could use Invoke-Command to get this done which has a default throttle limit of 32 nodes. This can be altered with the -Throttlelimit Parameter. That is one option. Here is another option i just quickly put together that targets all machines in the domain. Let me know if you have any questions:

$Computers = Get-ADComputer -Filter \* | select -ExpandProperty Name  
foreach ($computer in $Computers) {

if(Test-Connection -ComputerName $Computer -count 1 -Quiet){  
$Files = @{  
Files = “c:\temp\*”,  
“c:\windows\temp\*”,  
“c:\support\*”,  
“c:\windows\system32\ccm\cache\chd00329.8.system”,  
“c:\windows\system32\ccm\cache\chd00329.9.system”  
}

```
$Remove = New-Object -TypeName PsObject -Property $Files

$Remove.Files | Remove-Item -Recurse -Force -ErrorAction 'SilentlyContinue'

Write-Host "Files in $($files.files) have been removed from $Computer" -ForegroundColor Green

}

Else{

 Write-Host "$Computer is not online" -ForegroundColor Yellow

    }

```

}

---

<div class="post-metadata">

**Author:** ![jas\_wheel](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jas\_wheel](https://forums.powershell.org/u/jas_wheel)\
**Post date:** [June 15, 2015, 12:49am UTC](https://forums.powershell.org/t/pssession-many-computers/4354/3 "2015-06-15T00:49:38Z")

</div>

Thanks for the reply, you are absolutely right, this particular use case shouldn’t use pssession. I was attempting to use it incorrectly.

Appreciate the help

---

<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:45pm UTC](https://forums.powershell.org/t/pssession-many-computers/4354/4 "2024-05-16T20:45:08Z")

</div>

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


