PowerShell:- Out-GridView -PassThru | Set-Clipboard


Yesterday, I found myself struggling with a database project.

I thought let me capture some metadata, view it in a grid view and I will have more to go with.



Captured the data into a data table, pipelined it into GridView.

Got a good thing going.

As always got greedy.

Thought to capture in clipboard.


	powershell -- how do I create and populate a DataTable from a CSV file?

	Question:- Christian Bahnsen
	Answer:- Andreas Baumgarten
# Define the DataTable Columns
[System.Data.DataTable] $table = $null;

$table = New-Object system.Data.DataTable 'TestDataTable'

$columnFirstname = New-Object system.Data.DataColumn FirstName,([string]); 

$columnLastname = New-Object system.Data.DataColumn LastName,([string]); 

$columnID = New-Object system.Data.DataColumn ID,([int]); 

# Add a DataTable row
$row = $table.NewRow()
$row.FirstName= ("Bill")
$row.LastName= ("Gates")
$row.ID= [int](123456)

$table |  Out-GridView -PassThru | Set-Clipboard


Everything works well.

Your Data table is populated.

The data is shown in a grid view.

You select all of the rows in the Grid View.

Engage the OK button on the grid view.

Upon the grid view closing, you paste your captured data into a document.


All you see is




Code Sharing



Powershell:- DataTable & GridViewLink


Will dedicate this post to Christian Bahnsen and Andreas Baumgarten.



Christian Bahnsen

Christian Bahnsen for asking a nice straight forward question.

It goes…



I want to try to do a bulk insert to a sql table.

I found an article at bulk-copy-data-sql-server-powershell

that discusses using a DataTable as a source. I’ve never worked with DataTables before.

I’ve saved the data as a CSV that has the same column names as the destination table.

So it looks like I need to create the DataTable first:

$a = New-Object System.Data.DataTable
$a | Get-Member

But when I run Get-Member it tells me “You must specify an object for the Get-Member cmdlet.” I thought the New-Object statement would have created an object.

Then I need to populate the object. I was hoping something like

Get-Content filename.csv could be piped into the DataTable.

Do I need to define columns for the DataTable first?

Then how can I populate the DataTable from the CSV?

Thanks in advance for any assistance.

Christian Bahnsen


Andreas Baumgarten



Just now discovered that Andreas Baumgarten is a Microsoft MVP.

So you know you gotta to Lean Back.

#1 Draft Pick Like Eli Manning.



Unfortunately, as good a thing as the data table is, it does not interact with a grid view in such a way that one can capture the raw data; less to talk of its richness.

You will have to try other things to capture your data in a clipboard.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s