Ready to get started?

Learn more about the CData ADO.NET Provider for Box or download a free trial:

Download Now

Automate Box Integration Tasks from PowerShell

Are you looking for a quick and easy way to access Box data from PowerShell? We show how to use the Cmdlets for Box and the CData ADO.NET Provider for Box to connect to Box data and synchronize, automate, download, and more.

The CData Cmdlets for Box are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Box.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Box API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Box data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Box. To access Box data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Box.

After obtaining the needed connection properties, accessing Box data in PowerShell consists of three basic steps.

Box uses the OAuth standard to authenticate. To authenticate to Box, you will need to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL by registering an app. See the "Getting Started" chapter of the help documentation for a guide to using OAuth.

PowerShell

  1. Install the module:

    Install-Module BoxCmdlets
  2. Connect:

    $box = Connect-Box -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL"
  3. Search for and retrieve data:

    $id = "123" $files = Select-Box -Connection $box -Table "Files" -Where "Id = `'$Id`'" $files

    You can also use the Invoke-Box cmdlet to execute SQL commands:

    $files = Invoke-Box -Connection $box -Query 'SELECT * FROM Files WHERE Id = @Id' -Params @{'@Id'='123'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Box\lib\System.Data.CData.Box.dll")
  2. Connect to Box:

    $conn= New-Object System.Data.CData.Box.BoxConnection("OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the BoxDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Name, Size from Files" $da= New-Object System.Data.CData.Box.BoxDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.size }

Update Box Data

PowerShell

Update-Box -Connection $Box -Columns @('Name','Size') -Values @('MyName', 'MySize') -Table Files -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Box.BoxCommand("UPDATE Files SET Id='123' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.Box.BoxParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Box Data

PowerShell

Add-Box -Connection $Box -Table Files -Columns @("Name", "Size") -Values @("MyName", "MySize")

ADO.NET

$cmd = New-Object System.Data.CData.Box.BoxCommand("INSERT INTO Files (Id) VALUES (@myId)", $conn) $cmd.Parameters.Add(new System.Data.CData.Box.BoxParameter("@myId","123")) $cmd.ExecuteNonQuery()

Delete Box Data

PowerShell

Remove-Box -Connection $Box -Table "Files" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Box.BoxCommand("DELETE FROM Files WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.Box.BoxParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()