Powershell Tip: Read a Text File

So building upon our last example, we would like to of course run this function for a certain list of servers. We could just hard code another script to use in order to write out all those servers….but that would be labor intensive and we want to simplify! So the easier answer would be to just keep a list of servers in a text file. Easy to update…Easy to maintain. Then we would consume this list in our script and call our little backup function recursively.

So here we go….First, you would create a simple list of server in a text file. Place that file in your scripts directory. It should contain one server entry per line. Now let’s put together our script…

# Sample Script to do monitoring

. ./SQLScripts_Lib.ps1

#Read in a list of servers

foreach ($srv in Get-Content “C:\SQLScripts\ServerList.txt”)





That’s it. Completely too simple. The Get-Content call reads in the contents of the file that it is given(full path please). We stick this in a variable and pass it off to our function. Now all we have to do if we add a server is update a simple text file.

Hope this helps some people out!