How do I use MDT with PowerShell scripts?

Applies to: Microsoft Deployment Toolkit (MDT) 8456 and later
Last updated: May 2025
Problem
You want to enhance or customize MDT deployments by running PowerShell scripts—either to set variables, configure Windows, collect logs, or perform custom post-deployment tasks.
Solution
MDT supports running PowerShell scripts as part of Task Sequences or during deployment via command-line steps. Scripts can be run at any stage, and MDT exposes useful environment variables to PowerShell.
Step-by-Step Guide
1. Prepare Your PowerShell Script
Create a .ps1
script with your desired logic. Example: Set-RegistrySettings.ps1
:
Set-ItemProperty -Path "HKLM:\Software\Contoso" -Name "SupportContact" -Value "IT Dept"
Save the script in the MDT Scripts folder:
<DeploymentShare>\Scripts\
2. Add the Script to the Task Sequence
- Open Deployment Workbench
- Go to Task Sequences > Right-click your Task Sequence > Properties
- Select the Task Sequence tab
Step to Add:
- Click Add > General > Run PowerShell Script
- Set:
- Name: e.g.,
Configure Registry Settings
- Command line:
powershell.exe -ExecutionPolicy Bypass -File "%SCRIPTROOT%\Set-RegistrySettings.ps1"
- Name: e.g.,
%SCRIPTROOT%
automatically resolves to the MDT Scripts folder during deployment.
3. Access MDT Variables in PowerShell (Optional)
MDT variables are exposed via the ZTIUtility
COM object:
$TS = New-Object -ComObject Microsoft.SMS.TSEnvironment
$ComputerName = $TS.Value("OSDComputerName")
You can also set variables dynamically:
$TS.Value("CustomVar") = "Value123"
4. Conditional Execution (Optional)
Add conditions under the Options tab of the step:
- Based on computer model, location, role, or variable value
- Example:
- Run script only on laptops:
SELECT * FROM Win32_Battery
- Run script only on laptops:
Use Cases for PowerShell in MDT
- Rename computers using complex naming logic
- Log deployment details to a server
- Add custom registry keys or certificates
- Install software not supported natively in MDT
- Set environment or domain policies post-deployment
- Copy post-install files or drivers
Notes
- Always test scripts independently before adding them to MDT
- Use
Write-Output
orOut-File
to create custom logs - PowerShell logs can be found under:
C:\MININT\SMSOSD\OSDLOGS\
- Or redirected to a custom folder using your script