Finding SQL Servers with PowerShell, Part Deux

A reader named "Ben" wrote me and said that he looked at my script in PowerShell that finds SQL Server Instances using the WMI provider yesterday and he came up with another method. Here’s what he wrote:

Hi there,

I wasn’t able to find a way to post a comment to the blog post, but I was wondering why something like the following code wouldn’t be better:

   1: # Replace with your computer, or create a function from the code just below the $machine_name line below:
   3: $machine_name = "bwoody1"
   6: $sql_servers = get-wmiobject -class win32_service -computer $machine_name
   7: $sql_servers | where { $ -like 'MSSQL*' -and $_.Description -like '*transaction*'} | select name


The advantage that I see is that you don’t have to have powershell installed on each machine.  You can have a text file of server names and populate $machine_name through a read of that file. 

Thanks so much for your blog…I really learn a lot from reading it




Thanks, Ben!  The great thing about PowerShell is that there are so many ways to do similar things, based on your goals. Keep in mind with Ben’s approach you’ll need to be able to use the WMI provider on the target machine, which has security implications as well as the fact that WMI calls like this might be blocked by your security policies.

As always, this warning applies to any script you find anywhere, including here.

Comments (1)

  1. rhyscampbell says:

    SQL Sevrer discovery using Powershell and the InstalledInstances registry key