Skip to content

Out-DataTable

SYNOPSIS

Creates a DataTable for an object

SYNTAX

Out-DataTable [-InputObject] <PSObject[]> [-NonNullable <String[]>] [<CommonParameters>]

DESCRIPTION

Creates a DataTable based on an object's properties.

EXAMPLES

EXAMPLE 1

$dt = Get-psdrive | Out-DataTable

This example creates a DataTable from the properties of Get-psdrive and assigns output to $dt variable

EXAMPLE 2

Get-Process | Select Name, CPU | Out-DataTable | Invoke-SQLBulkCopy -ServerInstance $SQLInstance -Database $Database -Table $SQLTable -force -verbose

Get a list of processes and their CPU, create a datatable, bulk import that data

PARAMETERS

-InputObject

One or more objects to convert into a DataTable

Type: System.Management.Automation.PSObject[]
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False

-NonNullable

A list of columns to set disable AllowDBNull on

Type: System.String[]
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: @()
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

Object

Any object can be piped to Out-DataTable

OUTPUTS

System.Data.DataTable

NOTES

Adapted from script by Marc van Orsouw and function from Chad Miller Version History v1.0 - Chad Miller - Initial Release v1.1 - Chad Miller - Fixed Issue with Properties v1.2 - Chad Miller - Added setting column datatype by property as suggested by emp0 v1.3 - Chad Miller - Corrected issue with setting datatype on empty properties v1.4 - Chad Miller - Corrected issue with DBNull v1.5 - Chad Miller - Updated example v1.6 - Chad Miller - Added column datatype logic with default to string v1.7 - Chad Miller - Fixed issue with IsArray v1.8 - ramblingcookiemonster - Removed if($Value) logic. This would not catch empty strings, zero, $false and other non-null items - Added perhaps pointless error handling

https://github.com/RamblingCookieMonster/PowerShell

Invoke-SQLBulkCopy

Invoke-Sqlcmd2

New-SQLConnection

Back to top