Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions Links


SQL Server DBA Jobs

Read a script file into Powershell CommandText with get-content

18 November,2014 by Tom Collins

Question: How can I   read  the  script contents into the Powershell  CommandText ?

Normally I place  the query text    with the Powershell CommandText , but on this occasion I want to read a large query from a sql file.



$sql.CommandText = "SELECT name from sys.sysdatabases” 


Answer: Reading the contents from a script file is a good idea as it allows you to reuse your script library and maintain them in a script library.

Use CommandText to read a script file



$sqlCommand.CommandText = get-content c:\scripts\SQLText.sql


See Also other Powershell scripts

Powershell Scripts for DBA

Powershell , Excel charts and data presentation

Powershell to HTML

Powershell sql server security audit

Author: Tom Collins (


Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.


Post a comment on Read a script file into Powershell CommandText with get-content | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer