• Much simpler and more efficient version (only one round-trip to the SQL server).
    If you wanted to update the table with the current state, the valueList could be used to form a MERGE statement.


    # Get all services to a list of VALUEs for insert
    # -join ensures each row is separated with "comma, carriage return" except for the last one
    $valueList = (Get-Service | ForEach-Object { "( '$($_.Status)', '$($_.Name)', '$($_.DisplayName)')" }) -join ",`n"
    $insertquery=@"
    INSERT INTO [dbo].[ServiceTable]
        ([Status]
        ,[Name]
        ,[DisplayName])
      VALUES
    $valueList
    "@
    Invoke-SQLcmd -ServerInstance 'KILIKOOD-PC\MSSQLSERVER,1433' -query $insertQuery -U sa -P test123 -Database Fantasy