# Need a script to use a threading.

**URL:** <https://forums.powershell.org/t/need-a-script-to-use-a-threading/2716>\
**Category:** PowerShell Help\
**Created:** [June 17, 2014, 7:29pm UTC](https://forums.powershell.org/t/need-a-script-to-use-a-threading/2716 "2014-06-17T19:29:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sourav-m](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@sourav-m](https://forums.powershell.org/u/sourav-m)\
**Post date:** [June 17, 2014, 7:29pm UTC](https://forums.powershell.org/t/need-a-script-to-use-a-threading/2716/1 "2014-06-17T19:29:45Z")

</div>

Hi All,

I want to run a T-SQL batch statement (select statement) in 1000+ servers. But if i run the script using regular powershell, it will take a long time as it will refer the serverlist .txt file to read each server entry, then it will call a powershell script where each server will be passed as a loop then the results will be copied to an output file.

Instead of that, I think to use PS thread where a bunch of 10 servers will be taken a at a time, so that the select statement which I want to run against the servers would be completed sooner.

Reply me with your inputs.

Thanks.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 17, 2014, 11:46pm UTC](https://forums.powershell.org/t/need-a-script-to-use-a-threading/2716/2 "2014-06-17T23:46:58Z")

</div>

PowerShell is a single threaded application. Consider using background jobs.

---

<div class="post-metadata">

**Author:** ![martin-nielsen](https://avatars.discourse-cdn.com/v4/letter/m/bbce88/32.png) [@martin-nielsen](https://forums.powershell.org/u/martin-nielsen)\
**Post date:** [June 18, 2014, 12:00am UTC](https://forums.powershell.org/t/need-a-script-to-use-a-threading/2716/3 "2014-06-18T00:00:36Z")

</div>

You could compress your script into a single T-SQL statement instead of running 1000+ SELECT queries

```
$servers = Get-Content servers.txt
$servers = $servers -join "','"
$query = "SELECT * FROM table WHERE servername IN ('$servers')
```

Maybe?

---

<div class="post-metadata">

**Author:** ![sqldba](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sqldba/32/869_2.png) [@sqldba](https://forums.powershell.org/u/sqldba)\
**Post date:** [June 18, 2014, 6:10am UTC](https://forums.powershell.org/t/need-a-script-to-use-a-threading/2716/4 "2014-06-18T06:10:33Z")

</div>

Have you looked into background runspaces?

There’s a great into to the concept at [http://learn-powershell.net/2012/05/13/using-background-runspaces-instead-of-psjobs-for-better-performance/](http://learn-powershell.net/2012/05/13/using-background-runspaces-instead-of-psjobs-for-better-performance/)  
  
I’ve also got background runspaces heavily used in [url][https://sqlpowerdoc.codeplex.com/[/url]](https://sqlpowerdoc.codeplex.com/%5B/url%5D) - feel free to look at the code and use the relevant parts as needed to get up to speed on how runspaces work.

---

<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:47pm UTC](https://forums.powershell.org/t/need-a-script-to-use-a-threading/2716/5 "2024-05-16T20:47:55Z")

</div>

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


