Get output of Array into a SQL Server Table

  • Below is my sample powershell code. Can i take output of an array and insert into  a table in sql server.



    cls

    $filepath='C:\Temp\Backup'

    #Get-ChildItem -path $filepath -Recurse -Include *.bak

    #$allfiles=@(Get-ChildItem -path $filepath -Recurse -Include *.bak | select name,length ,lastwritetime )

    $allfiles=@(Get-ChildItem -path $filepath -Recurse -Include *.bak | sort LastWriteTime -Descending | select name,length ,lastwritetime | select -First 2 )

    $allfiles




  • A few different ways. One quick way using the SQLServer module is to just add a Write-SqlTableData cmdlet at the end of the script you started:

    Write-SqlTableData -serverInstance YourInstanceName -database YourDatabaseName -TableName YourTableName -SchemaName YourSchemaName -InputData $allfiles

    You can find examples of different methods in the following article:

    6 methods to write PowerShell output to a SQL Server table

    Sue

  • Works. Thank you

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply