PsSqlClient is a compiled PowerShell module for SQL Server and Azure SQL workflows commonly handled by SQLCMD and BCP. It provides PowerShell commands for connecting to databases, running SQL commands and stored procedures, reading scalar values, and bulk-copying PowerShell objects into tables. It is part of PsSqlUtils.
For command details, visit the online documentation.
Install the module from the PowerShell Gallery:
Install-Module -Name PsSqlClient -Scope CurrentUserAlternatively, build and install it from source:
- Clone this repository.
- Install InvokeBuild and the project dependencies.
- Run the installation task from the repository root:
Invoke-Build InstallSee the command reference for command details and additional examples.
When using Az.Accounts in the same PowerShell session, import it before PsSqlClient to avoid Azure assembly conflicts:
Import-Module Az.Accounts
Import-Module PsSqlClientIf PsSqlClient was loaded first and Azure authentication fails, start a new PowerShell session and import the modules in this order. Az.Accounts is not required for every Azure authentication method. See Connect-TSqlInstance for authentication options.
# Connect to SQL Server LocalDB using the current Windows login.
Connect-TSqlInstance -DataSource '(LocalDb)\MSSQLLocalDB'
# Create a temporary table with columns matching the CSV file.
Invoke-TSqlCommand 'CREATE TABLE #Test (Id INT NULL, Name NVARCHAR(MAX))'
# Import the CSV rows into the table.
Import-Csv 'test.csv' | Export-TSqlTable '#Test'This example requires SQL Server LocalDB and a test.csv file with Id and Name columns.
# Connect to SQL Server LocalDB using the current Windows login.
Connect-TSqlInstance -DataSource '(LocalDb)\MSSQLLocalDB'
# Get a scalar value from the database.
[string] $databaseName = Get-TSqlValue 'SELECT DB_NAME()'# Connect to SQL Server LocalDB using the current Windows login.
Connect-TSqlInstance -DataSource '(LocalDb)\MSSQLLocalDB'
# Get table metadata from master, then sort and select the first five rows.
Invoke-TSqlProcedure 'sp_tables' @{ 'table_qualifier' = 'master' } |
Sort-Object TABLE_NAME |
Select-Object -First 5| Command | Description |
|---|---|
| Connect-TSqlInstance | Open a SQL Server or Azure SQL connection. |
| Disconnect-TSqlInstance | Close a connection. |
| Test-TSqlConnection | Test a connection and its current database. |
| Invoke-TSqlCommand | Run SQL text, a SQL file, or a stored procedure and return result rows. |
| Invoke-TSqlProcedure | Run a stored procedure, SQL text, or a SQL file and return result rows. |
| Get-TSqlValue | Return the first value from a SQL command or stored procedure. |
| Export-TSqlTable | Bulk-copy pipeline objects into a SQL table. |
See the changelog file.
- See the architecture decision records for significant architectural decisions.
- This is a compiled .NET 8 PowerShell module. Its cmdlets are implemented in C# and use Microsoft.Data.SqlClient.
- VSCode is recommended as IDE. VSCode Tasks are configured.
- Build automation is based on InvokeBuild
- Documentation is based on platyPs
- Test automation is based on Pester
- Commands are named based on Approved Verbs for PowerShell Commands
- This project uses git-flow.
- This project uses keep a changelog.
- This project uses PsBuildTasks.
The build scripts require InvokeBuild. If it is not installed, install it with the command Install-Module InvokeBuild -Scope CurrentUser.
You can build the module using the VS Code build task or with the command Invoke-Build Build.
The tests are based on Pester. If it is not installed, install it with Install-Module Pester -Force -SkipPublisherCheck. Database tests use PsSqlTestServer 1.11.0, which can be installed with Install-Module PsSqlTestServer -RequiredVersion 1.11.0 -Scope CurrentUser -Force. Docker-based tests create a SQL Server container; install Docker and PSDocker (Install-Module PSDocker -Scope CurrentUser) to run them. Other test groups may require LocalDB or Azure SQL credentials.
For Azure SQL tests, configure a subscription using PSSQLTESTSERVER_AZURE_SUBSCRIPTION, a settings file, or the current Azure context. For example, create the ignored test settings file with New-SqlTestSettings -AzureSubscription <name-or-id> -Directory .\test from the project root.
The export, disconnect, value, command, procedure, and connection tests select LocalDB, then Docker, then Azure. To allow Azure fallback, set AllowBillableResources to the JSON boolean true in .PsSqlTestServer.settings.json, or set PSSQLTESTSERVER_ALLOW_BILLABLE_RESOURCES=true. A subscription alone does not grant consent. Azure also requires the Az modules, an authenticated context, the existing PsSqlTestServer resource group, and permissions and network access to create and connect to Azure SQL resources.
Azure test resources may remain after a failure and continue to incur charges. Configure an Azure Cost Management budget with alerts before running these tests, and inspect and remove leftover resources afterward. Budget alerts do not automatically stop spending.
Run Azure tests in a fresh PowerShell session with Import-Module Az.Accounts before importing PsSqlClient or invoking build and test tasks.
The Azure ActiveDirectoryIntegrated test runs only on AD-domain-joined Windows machines. Domain membership alone does not guarantee success: the process also needs an identity configured for Entra integrated authentication and authorized for the test database. Entra-only and other non-domain-joined configurations are skipped.
The interactive Azure authentication test runs by default. To exclude it for unattended runs, use Invoke-Pester -Path .\test\Connect-Instance.Tests.ps1 -ExcludeTagFilter Interactive.
For local testing use the VSCode test tasks or execute the test scripts directly or with Invoke-Pester.
The InvokeBuild test tasks are for CI and do not generate console output.
- Create a release branch using git-flow.
- Update the version number in the module manifest.
- Update the changelog in CHANGELOG.md.
- If you want to create a pre-release.
- Push the release branch to github, to publish the pre-release to PsGallery.
- Finish release using git-flow.
- Check if tags are not pushed to github.
- Check if the release branch is deleted on github.
- Create the release on github.