I needed to run some SQL commands from my local machine connecting to a remote SQL server, using something like:

(Invoke-Sqlcmd -ServerInstance $ServerInstance -InputFile "C:\WEBTECHY\SQL\databaseusers.sql" -Variable $VariableArray | ConvertTo-XML -NoTypeInformation).save("C:\WEBTECHY\BACKUPS\$ServerInstance\$DatabaseName-Logins.xml")

However, it came back with:

System.Management.Automation : The Windows PowerShell snap-in 'SqlServerProviderSnapin' is not installed on this machine.

I tried installing the following features (using the CD install for SQL Server 2008):

# Client Tools Connectivity
# Management Tools - Basic

Originally it wouldn't install the snap-in when I ran:

ps> add-pssnapin SqlServerProviderSnapin

And I thought that maybe you had to install the full Database Engine. However, I uninstalled the database engine, and was able to run the following command instead:

ps> add-pssnapin SqlServerProviderSnapin100

So as long as there wasn't anything left from the Database Engine install and uninstall, it should be just the snap-in name you need to use with those two items listed above (presumably only one of them is needed but to be sure I installed them both).

After that, running:

ps> get-pssnapin

then give me:

Name        : Microsoft.PowerShell.Diagnostics
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains Windows Eventing and Performance Counter cmdlets.

Name        : Microsoft.WSMan.Management
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets (such as Get-WSManInstance and Set-WSManInstance) that a
              re used by the Windows PowerShell host to manage WSMan operations.

Name        : Microsoft.PowerShell.Core
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell.

Name        : Microsoft.PowerShell.Utility
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains utility Cmdlets used to manipulate data.

Name        : Microsoft.PowerShell.Host
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets (such as Start-Transcript and Stop-Transcript) that are
              provided for use with the Windows PowerShell console host.

Name        : Microsoft.PowerShell.Management
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains management cmdlets used to manage Windows components.

Name        : Microsoft.PowerShell.Security
PSVersion   : 2.0
Description : This Windows PowerShell snap-in contains cmdlets to manage Windows PowerShell security.

Name        : sqlserverprovidersnapin100
PSVersion   : 2.0
Description : SQL Server Provider


I am unable to confirm the difference between SQLServerProviderSnapin100 and SQLServerProviderSnapin unfortunately. Anyone?

FYI, here is a little function I use to add the snap-in:

function InstallRequiredSQLSnapIns() {
    try {
        # Add the required snap-ins for SQL Server
        Write-Host "Installing required snap-ins ..."
        Add-Pssnapin SqlServerProviderSnapin100 -ErrorAction Stop
        Add-Pssnapin SqlServerCmdletSnapin100 -ErrorAction Stop
        #Add-Pssnapin SqlServerProviderSnapin100 -ErrorAction SilentlyContinue
        #Add-Pssnapin SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue
        return $true
    } catch {
        # E.g. System.Management.Automation : The Windows PowerShell snap-in 'SqlServerProviderSnapin' is not installed on this machine.
        # E.g. System.Management.Automation : Cannot add Windows PowerShell snap-in SqlServerProviderSnapin100 because it is already added. Verify the name of the snap-in and try again.
        [string]$ErrorString = [string]$_.Exception.Message
        if ($ErrorString.contains("already added")) {
            return $true
        } else {
            Write-Host $_.Exception.Source,":", $ErrorString
            return $false
        }
    }
}


InstallRequiredSQLSnapIns

This then won't throw an error if the snap-in has already been added (you could use -ErrorAction SilentlyContinue but then you wouldn't get notified if there was an issue).