> 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/1.2/powershell-modules/scripting-for-osd.md).

# Scripting for OSD

### Connecting to the Task Sequence Environment

To leverage the variables in the task sequence, or create task sequence variables with PowerShell, you'll need to add a simple line into your script:

```powershell
Import-Module DeployR.Utility
```

Once you've done that, you can start pulling in variables you've set in the Task Sequence, or ones that are auto generated.  You can access the variables by using ${TSEnv:VariableName}.  Here is an example of pulling three Task Sequence variables into standard PowerShell variables to be used later in the script.

```powershell
[String]$MakeAlias = ${TSEnv:MakeAlias}
[String]$ModelAlias = ${TSEnv:ModelAlias}
[int]$OSImageBuild = ${TSEnv:OSImageBuild}

if ($MakeAlias -ne "Dell" -and $MakeAlias -ne "Lenovo" -and $MakeAlias -ne "HP" -and $MakeAlias -ne "Panasonic Corporation" -and $MakeAlias -ne "Microsoft") {
    Write-Host "MakeAlias must be Microsoft, Dell, Lenovo, Panasonic or HP. Exiting script."
    Exit 0
}
```

To create Task Sequence variables that can then be used by other steps in your task sequence, once you've connected into your TS environment by using the Import-Module DeployR.Utility command, you will simply set the variable:

```
$tsenv:LineOfBusiness = "RetailAgents"
$tsenv:HyperVHost = (Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters' -Name "HostName" -ErrorAction SilentlyContinue)
```

<figure><img src="/files/07BkP2nubv9YcUZ4RIfr" alt=""><figcaption></figcaption></figure>

For this example, I've added a step that runs those two lines, we'll take a look at the logs and variables:

<figure><img src="/files/2X0tn5wv5kVHrT2sZkJo" alt=""><figcaption></figcaption></figure>


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.2pintsoftware.com/deployr/1.2/powershell-modules/scripting-for-osd.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
