> For the complete documentation index, see [llms.txt](https://documentation.2pintsoftware.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://documentation.2pintsoftware.com/deployr/automation-and-powershell/scripting-for-deployr-server.md).

# Scripting for DeployR Server

Using the PowerShell Module to automate tasks or do lookups.  Remember to use PowerShell 7!

### Authenticating to the DeployR server for scripting

Depending on your DeployR security configuration, you may need to authenticate with `Connect-DeployR` before running commands. This example uses passcode authentication.

```powershell
Import-Module 'C:\Program Files\2Pint Software\DeployR\Client\PSModules\DeployR.Utility'
Connect-DeployR -Passcode PASSWORD
```

Without `-Passcode` or `-Credential`, `Connect-DeployR` tries anonymous access, then Windows integrated authentication (when it is enabled on the server), then Entra ID. To sign in as a specific Windows account, pass `-Credential (Get-Credential)`. For Entra ID, it opens your browser and waits up to five minutes for the sign-in to finish, or shows a device code if the server is configured for device code flow. Starting with DeployR 1.4, the browser sign-in listens on a random local port instead of port 7285.

For a list of all the commands in the module, see [DeployR.Utility module reference](/deployr/automation-and-powershell/deployr-utility-module-reference.md).

After authenticating, you can use PowerShell to retrieve information and content. Common use cases include backing up items for disaster recovery and importing items into another DeployR instance, such as when promoting content from test to production.

You can create backup scripts that filter for specific items or export everything. Retrieving and exporting content requires only a few functions.

### Exporting Content Items

When run without parameters, `Get-DeployRContentItem` returns all content items. You can pipe the results to `Export-DeployRContentItem`.

```powershell
Get-DeployRContentItem | Export-DeployRContentItem -DestinationFolder "D:\Backups\Temp\"
```

<figure><img src="https://content.gitbook.com/content/8m0VARX9oyZ2zXWCdPgr/blobs/N9o6e2L3yp9addvH5Blw/image.png" alt=""><figcaption></figcaption></figure>

Add `-ExcludeDeprecated` to leave out content item versions with a status of **Deprecated**:

```powershell
Get-DeployRContentItem | Export-DeployRContentItem -DestinationFolder "D:\Backups\Temp\" -ExcludeDeprecated
```

This sample code block filters out built-in packages (-notlike '0000000-\*') and filters on a purpose of "Other".

```powershell
#Backup DeployR content items
Write-Host "Backing up DeployR content items..." -ForegroundColor Yellow
$ContentItems = Get-DeployRContentItem | Where-Object {$_.id -notlike '00000000-*'} | Where-Object {$_.contentItemPurpose -match "Other"} 
$ContentItems | ForEach-Object {
    write-host "Backing up content item: $($_.name) | $($_.id)" -ForegroundColor Cyan
    Export-DeployRContentItem -Id $_.id -DestinationFolder "D:\Backups\ContentItems\$($_.name)-$($_.id)"
}
```

### Exporting Step Definitions

If you are creating step definitions for your organization, these are also worth backing up. To get the step definition data, you will use the Get-DeployRMetaData function with the -Type StepDefinition parameter. You can then export it with the Export-DeployRStepDefinition function.

```powershell
Get-DeployRMetadata -Type StepDefinition
```

Here is a sample code block used to back up all step definitions that are not built in (-notlike '0000\*')

```powershell
#Backup DeployR step definitions
Write-Host "Backing up DeployR step definitions..." -ForegroundColor Yellow
$Steps = (Get-DeployRMetadata -Type StepDefinition | Where-Object {$_.id -notlike '0000*'})
$Steps | ForEach-Object {
    write-host "Backing up step definition: $($_.name) | $($_.id)" -ForegroundColor Cyan
    Export-DeployRStepDefinition -Id $_.id -DestinationFolder "D:\Backups\StepDefinitions\$($_.name)-$($_.id)"
}
```

### Exporting Task Sequences

To export a task sequence, use Get-DeployRMetaData with the -Type TaskSequence parameter, then pipe that to Export-DeployRTaskSequence or run the Export-DeployRTaskSequence function with the Id parameter.

```powershell
Get-DeployRMetadata -Type TaskSequence
```

The example below uses the task sequence ID, which you can find with PowerShell or in the task sequence URL in the dashboard.

```powershell
$TSID = '102d275b-fee3-477b-aa07-a8f63ca2140c'
Export-DeployRTaskSequence -Id $TSID -DestinationFolder D:\TSBackups
```

Here is a sample code block for exporting task sequences that are not built in:

```powershell
#Backup DeployR task sequences
Write-Host "Backing up DeployR task sequences..." -ForegroundColor Yellow
(Get-DeployRMetadata -Type TaskSequence | Where-Object {$_.id -notlike '0000*'}) | ForEach-Object {
    write-host "Backing up task sequence: $($_.name) | $($_.id)" -ForegroundColor Cyan
    Export-DeployRTaskSequence -Id $_.id -DestinationFolder "D:\Backups\TaskSequences\$($_.name)-$($_.id)"
}
```

The exported task sequence will be a json file with the name of the GUID. It will not include the content items referenced, just the metadata of the task sequence.

<figure><img src="https://content.gitbook.com/content/8m0VARX9oyZ2zXWCdPgr/blobs/jPeiPbMDf8SqHyRGyJH0/image.png" alt=""><figcaption></figcaption></figure>

For an example script, see this one on the 2Pint Software GitHub: <https://github.com/2pintsoftware/2Pint-DeployR/blob/main/Samples/Webinars/ExportSpecificTaskSequenceMetaData.ps1>

### Importing Task Sequences

To import, you need an exported task sequence JSON file. Then run the import function like so:

```powershell
Import-DeployRTaskSequence -SourceFile D:\DeployRCIs\TEMP\102d275b-fee3-477b-aa07-a8f63ca2140c.json
```

### Clone Task Sequence

Cloning works the same way as importing, but you also specify -clone:

```powershell
Import-DeployRTaskSequence -SourceFile 102d275b-fee3-477b-aa07-a8f63ca2140c.json -clone
```

For an example script of the clone ability, see this script on the 2Pint Software GitHub. <https://github.com/2pintsoftware/2Pint-DeployR/blob/main/Samples/Webinars/CloningTaskSequenceDemo.ps1>

### Deleting items

`Remove-DeployRMetadata` deletes task sequences, step definitions, content items, individual versions, and boot images. Pass the type and the object to delete:

```powershell
# Delete a task sequence
Get-DeployRTaskSequence -Id 102d275b-fee3-477b-aa07-a8f63ca2140c | Remove-DeployRMetadata -Type TaskSequence

# Delete the deprecated versions of a content item
(Get-DeployRContentItem -Id $id).versions | Where-Object { $_.status -eq "Deprecated" } | Remove-DeployRMetadata -Type ContentItemVersion
```

Deleting cannot be undone, so export anything you might need first.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://documentation.2pintsoftware.com/deployr/automation-and-powershell/scripting-for-deployr-server.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
