- Add and remove Windows with drivers in MDT with PowerShell - Fri, Jun 24 2016
- Creating an EXE for your PowerShell scripts using WinRAR - Thu, May 19 2016
- Using the Microsoft Deployment Toolkit (MDT) as a portable application - Tue, May 10 2016
Adding items
Adding items to deployment share can be achieved in three steps:
- Start by importing the Microsoft Deployment Toolkit This “.psd1” file is located in the folder C:\Program Files\Microsoft Deployment Toolkit\bin.
- Create a PSDrive that will be mapped to the deployment share folder.
- Add your item using a specific cmdlet.
In this article, I’ll focus on the cmdlet import-mdtoperatingsystem and import-mdtdriver that will add Windows and drivers, respectively.
Consider the following variables for all the scripts used in this article:
- $module: location of the MDT module, c:\Program Files\Microsoft Deployment Toolit\Bin
- $deploymentshare: location of your Deployment Share
MDT also allows you create folders in your deployment share. For instance, you could create a folder "Windows 10" for you Windows 10 images. As mentioned above, we first need to import the MDT module, then create the PSDrive. To create a Windows 10 folder we’ll use the cmdlet new-item. The PowerShell code snipped below performs these tasks:
Import-Module "$module\MicrosoftDeploymentToolkit.psd1" New-PSDrive -Name "DS001" -PSProvider MDTProvider -Root "$deploymentShare" New-Item -path "DS001:\Operating Systems" -enable "True" -Name "Windows10" -Comments "" -ItemType "folder" -Verbose
So far, we’ve learned how to add items to the deployment share with PowerShell. Now let’s see how to add and remove an operating system.
Adding Windows
All operating system in MDT are located in the Operating Systems node of the deployment share. As shown in the screenshot below, three options are available when you add an OS.
- Full set of source files > Source files from a Windows DVD, CD, or equivalent.
- Custom image file > Use a captured image (WIM)
- Windows Deployment Services images > Use an image available on a WDS server
When you add an OS, the following two things will happen:
- The OS sources will be added to the Operating Systems folder of your deployment share.
- Information about the OS will be added to the Control\OperatingSystems.xml
Instead of adding an OS with the GUI, you can use PowerShell to automate the task. When you add an OS with MDT, a click on the View Script button shows you the corresponding PowerShell script.
As mentioned in the first part, to add our OS we’ll use the cmdlet import-mdtoperatingsystem. Following are the PowerShell scripts to add an OS. We’ll import our OS in the Windows 10 node previously created.
Script to add an OS with a full set of source files
Import-Module "$module\MicrosoftDeploymentToolkit.psd1" New-PSDrive -Name "DS001" -PSProvider MDTProvider -Root "$deploymentshare" import-mdtoperatingsystem -path "DS001:\Operating Systems\Windows 10" -SourcePath "C:\win_x64" -DestinationFolder "Windows 10 Enterprise x64" –Verbose
Script to add OS with a custom image file
Import-Module "$module\MicrosoftDeploymentToolkit.psd1" New-PSDrive -Name "DS001" -PSProvider MDTProvider -Root "$deploymentShare" import-mdtoperatingsystem -path "DS001:\Operating Systems\Windows 10" -SourceFile "C:\win_x64\sources\install.wim" -DestinationFolder "W10x64_wim" –Verbose
Script to add OS from the WDS
Import-Module "$module\MicrosoftDeploymentToolkit.psd1" New-PSDrive -Name "DS001" -PSProvider MDTProvider -Root "$deploymentShare" import-mdtoperatingsystem -path "DS001:\Operating Systems\Windows 10" -WDSServer "10.139.36.161" –Verbose
Common switches:
- -Name => Name of the PSDrive
- -Root => The path to your deployment share
- -Path => Folder in the OS node from the DeploymentWorkBench
- -Verbose => Add an entry to log files Audit.log
Specific switches used to add an OS:
- Adding with a full set of sources files: switch -SourcePath
- Adding with a custom image file: switch -SourceFile
- Adding from a WDS Server: switch -WDSServer
As you can see in the following screenshot, the next script allows you to add an OS using one of three options
- Add an OS with full set of source files
- Add an OS from a captured image (WIM file)
- Add an OS from a WDS server
If you have chosen full sources it will (a) ask for your OS sources; (b) and then for the folder that will be created in the Operating Systems directory from your deployment share.
If you have chosen add an OS from a captured image it will (a) ask for the path of your WIM file; (b) and then ask for the folder that will be created in the OS folder from your deployment share.
After entering the required information, the process will start.
Below is the entire script for adding an OS to MDT.
$module = "C:\Program Files\Microsoft Deployment Toolkit\Bin\MicrosoftDeploymentToolkit.psd1" $deploymentshare = "C:\DeploymentShare" Import-Module $module $PSDrive_Test = get-psdrive If ($PSDrive_Test -eq "DSManager") { Remove-PSDrive -Name "DSManager" New-PSDrive -Name "DSManager" -PSProvider MDTProvider -Root $deploymentshare write-host "The PSDrive DSManager has been mapped to your Deployment Share" } Else { New-PSDrive -Name "DSManager" -PSProvider MDTProvider -Root $deploymentshare write-host "The PSDrive DSManager has been mapped to your Deployment Share" } [int]$Menu_OS = 0 while ( $Menu_OS -lt 1 -or $Menu_OS -gt 4 ){ Write-host "" Write-host "Update your Deploymentshare Choice" Write-host "====================================================================" Write-host "1. Add an Operating System with full set of source files" Write-host "2. Add an Operating System from a captured image (WIM file)" Write-host "3. Add an Operating System from a Windows Deployment Services server" Write-host "4. Quit and exit" Write-host "" [Int]$Menu_OS = read-host "Please enter an option 1 to 4" } Switch( $Menu_OS ){ 1 { Write-host "Import OS with full set of sources files" Write-host "====================================================================" $Sources_Full = Read-Host -Prompt '***** Select your OS sources' $Destination_folder = Read-Host -Prompt '***** Type your destination folder' import-mdtoperatingsystem -path "DSManager:\Operating Systems" -SourcePath $Sources_Full -DestinationFolder $Destination_folder –Verbose } 2 { Write-host "Import OS from a captured image (WIM file)" Write-host "====================================================================" $Sources_WIM = Read-Host -Prompt '***** Select your WIM file' $Destination_folder = Read-Host -Prompt '***** Type your destination folder' import-mdtoperatingsystem -path "DSManager:\Operating Systems" -SourceFile $Sources_WIM -DestinationFolder $Destination_folder –Verbose } 3 { Write-host "Import OS from a WDS server" Write-host "====================================================================" $WDS_Server = Read-Host -Prompt '***** Type your WDS server address' import-mdtoperatingsystem -path "DS001:\Operating Systems\Windows 10" -WDSServer $WDS_Server –Verbose } 4{exit} }
Removing an operating system
As with the previous procedure, when you remove an OS from your deployment share using MDT, the View Script button shows you the corresponding PowerShell command:
remove-item -path "DS001:\Operating Systems\<OS_Name> -force –verbose
- -Path => The path to your deployment share
- -Force => Delete this item, even if there are copies in other folders.
- -Verbose => Add an entry to log files Audit.log
The <OS_Name> can be found in the Control\OperatingSystems.xml file. In this example the name is Windows 10 Enterprise in Windows 10 Enterprise x64 install.wim
<?xml version "1.0"> <oss> <os guid="{ac64dded-3476-49d2-8f0a-a30f258b1bee}" enable="True"> <Name>Windows 10 Enterprise in windows_10_x64 install.wim</Name> <CreatedTime>08/06/2016 13:15:20</CreatedTime> <CreatedBy>damien</CreatedBy> <LastModifiedTime>08/06/2016 13:15:20</LastModifiedTime> <LastModifiedBy>damien</LastModifiedBy> <Description>Windows 10 Enterprise</Description> <Platform>x64</Platform> <Build>10.0.10240.16384</Build> <OSType>Windows IBS</OSType> <Source>.\Operating Systems\windows_10_x64</Source> <IncludesSetup>True</IncludesSetup> <SMSImage>False</SMSImage> <ImageFile>.\Operating Systems\windows_10_x64\Sources\install.wim</ImageFile> <ImageIndex>1</ImageIndex> <ImageName>Windows 10 Enterprise</ImageName> <Flags>Enterprise</Flags> <HAL>acpiapic</HAL> <Size>12940</Size> <Language>en-US</Language> </os> </oss>
The below script allows you to remove an OS from your deployment share.
It’s composed of four parts:
- Asks for the OS name
- Loads the MicrosoftDeploymentToolkit module
- Creates a new PSDrive “DSManager” mapped to the deployment share
- Removes the OS
After you enter the OS name, the process will start.
Param ( [Parameter(Mandatory=$true)] [String]$OS_name ) $module = "C:\Program Files\Microsoft Deployment Toolkit\bin\MicrosoftDeploymentToolkit.psd1" $deploymentshare = "C:\DeploymentShare" Import-Module $module $PSDrive_Test = get-psdrive If ($PSDrive_Test -eq "DSOSmanager") { Remove-PSDrive -Name "DSOSmanager" New-PSDrive -Name "DSOSmanager" -PSProvider MDTProvider -Root $deploymentshare } Else { New-PSDrive -Name "DSOSmanager" -PSProvider MDTProvider -Root $deploymentshare } remove-item -path "DSOSmanager:\Operating Systems\$OS_name" -force –verbose
Adding drivers
The PowerShell cmldet used to add drivers to a deployment share is import-drivers.
The code below (provided from the View Script button) allows you to add drivers to a deployment share.
Import-Module "$module\MicrosoftDeploymentToolkit.psd1" New-PSDrive -Name "DS001" -PSProvider MDTProvider -Root "$deploymentShare" import-mdtdriver -path "DS001:\Out-of-Box Drivers" -SourcePath "<DriversPath>" -ImportDuplicates –Verbose
- -Name: name of the PSDrive to create
- -Root: The path to your deployment share
- -Path: Folder in the Operating System node from the DeploymentWorkBench
- -SourcePath: Sources of your drivers
- -ImportDuplicates: Use it if you want to import duplicates drivers (see below for more information)
- -Verbose: Add an entry to log files Audit.log
The ImportDuplicates parameter is the corresponding switch for the checkbox below. It ensures that drivers are imported even if they are duplicate drivers.
Removing drivers
We’ll also use the same PowerShell cmdlet remove-item to remove drivers from a deployment share.
Below is the script, provided from the View Script button.
remove-item -path "DS001:\Out-of-Box Drivers\<Driver_Name>" -force –verbose
- -Path: The path to your deployment share
- -Force: Delete this item, even if there are copies in other folders.
- -Verbose: Add an entry to log files Audit.log
The <Driver_Name> can be found in the Control\Drivers.xml file. In this sample it’ll be Synaptics Incorporated BiometricDevice synaWudfBioUsb.inf 5.0.87.06
Subscribe to 4sysops newsletter!
<?xml version "1.0"> <drivers> <driver guid="{2898956e-02d1-4914-b050-74266825a9f2}" enable="True"> <Name>Synaptics Incorporated BiometricDevice synaWudfBioUsb.inf 5.0.87.06</Name> <CreatedTime>20/06/2016 07:57:15</CreatedTime> <CreatedBy>damien</CreatedBy> <LastModifiedTime>20/06/2016 07:57:15</LastModifiedTime> <LastModifiedBy>damien</LastModifiedBy> <Manufacturer>Synaptics Incorporated</Manufacturer> <Version>5.0.87.06</Version> <Date>11/25/2015</Date> <Source>.\Out-of-box Drivers\BiometricDevice\synaWudfBioUsb_5.0.87.06_4CC9354503BF5A56D4F4FBB8372FB3E7A75A630D76E4B24C8E7868851D3F7C24\synaWudfBioUsb.inf</Source> <Class>BiometricDevice</Class> <Hash>4CC9354503BF5A56D4F4FBB8372FB3E7A75A630D76E4B24C8E7868851D3F7C24</Hash> <WHQLSigned>True</WHQLSigned> <Platform>x64</Platform> <OSVersion>6.2</OSVersion> <PNPId>USB\VID_138A&PID_0090</PNPId> </driver> </drivers>
Thank you for the article, it's a great help!