Using Powershell to Add Drivers to MDT

Recently I was asked to assist with our Microsoft Deployment Toolkit (MDT) deployment server because it has fallen out of love for a little while now. I have in the past used MDT to help numerous companies automate their builds and got some really good results from it.

I remoted into the server and opened the Deployment Workbench to find that there was very little organization of things that were put into it. I decided that it is better to redesign it than to fix it as it would take me much more time and the work that was done only covers a small segment of our client base. The first area that I decided to work on was the drivers and in doing so I needed to create a structure that would be easily maintainable and usable.

To start I created a folder inside of the Out-of-Box Drivers folder called Windows 10 x64. I then went to our RMM tool and pulled a report of all of the computers that we managed and exported it to Excel. I used a pivot table to get the quantities of each model, sorted by the highest quantity models, with a column for manufacturer and another for model. I then used this to create a CSV file that had the top 20 models that we supported with column A that listed the manufacturer and column B listed the model and saved it as MDTFoldersDrivers.csv in C:\temp.

I then used Powershell to create the folders for me in MDT.

The code below is where I begin. I start by creating a variable called $filepath that stores the location of where the CSV file is. The second line imports the contents of the CSV into the variable called $folders. Notice that the name is plural, this is not required but it helps for readability later.

$filepath = 'C:\Temp\MDTFoldersDrivers.csv'
$folders = Import-CSV $filepath 

In this section, I am importing the MDT module and creating a new drive called DS001 which has a path of D:\DeploymentShare.

Import-Module "C:\Program Files\Microsoft Deployment Toolkit\bin\MicrosoftDeploymentToolkit.psd1"
New-PSDrive -Name "DS001" -PSProvider MDTProvider -Root "D:\DeploymentShare"

 

In this section, we are now going to loop through the contents of the $folders variable which is actually an array. It holds many different objects and we are going to loop through them one at a time and place them in a newly created variable called $folder. This variable can be called anything but notice that I used the singular version of it.

The first thing we do is create two new variables to store the manufacturer and the model. We use what is called dot notation to to take the manufacturer property of the object in the $folder variable and place it in the $manufacturer variable.

The next step is to first use a IF statement to test to see if the folder for the manufacture already exists and if it does then just create the folder for the model. If the folder does not exist then the statement will be false and the ELSE will execute which first creates the manufacturer folder and then creates the model folder.

foreach ($folder in $folders){
    $manufacturer = $folder.Manufacturer
    $model = $folder.Model

    if (Test-Path "DS001:\Out-of-Box Drivers\Windows 10 x64\$manufacturer"){
        new-item -path "DS001:\Out-of-Box Drivers\Windows 10 x64\$Manufacturer" -enable "True" -Name $model -Comments "" -ItemType "folder" -Verbose
    } else {
        new-item -path "DS001:\Out-of-Box Drivers\Windows 10 x64\" -enable "True" -Name $Manufacturer -Comments "" -ItemType "folder" -Verbose
        new-item -path "DS001:\Out-of-Box Drivers\Windows 10 x64\$Manufacturer" -enable "True" -Name $model -Comments "" -ItemType "folder" -Verbose

    }
}

Below is the entire script...

$filepath = 'C:\Temp\MDTFoldersDrivers.csv'
$folders = Import-CSV $filepath
Import-Module "C:\Program Files\Microsoft Deployment Toolkit\bin\MicrosoftDeploymentToolkit.psd1"
New-PSDrive -Name "DS001" -PSProvider MDTProvider -Root "D:\DeploymentShare"

foreach ($folder in $folders){
    $manufacturer = $folder.Manufacturer
    $model = $folder.Model

    if (Test-Path "DS001:\Out-of-Box Drivers\Windows 10 x64\$manufacturer"){
        new-item -path "DS001:\Out-of-Box Drivers\Windows 10 x64\$Manufacturer" -enable "True" -Name $model -Comments "" -ItemType "folder" -Verbose
    } else {
        new-item -path "DS001:\Out-of-Box Drivers\Windows 10 x64\" -enable "True" -Name $Manufacturer -Comments "" -ItemType "folder" -Verbose
        new-item -path "DS001:\Out-of-Box Drivers\Windows 10 x64\$Manufacturer" -enable "True" -Name $model -Comments "" -ItemType "folder" -Verbose

    }
}

Leave a Reply

Your email address will not be published.