Tuesday, September 3, 2013

Setting Windows 7 AutoPlay settings via batch script.

The initial use case for this issue came up when I needed to deploy PowerDVD to a handful of workstations (150).  I ended up creating an initial application for deployment that worked wonderful.  However, it wasn't long before reports started coming in that users' DVD's would not play in Media Player.  At this point we realized that the AutoPlay settings for DVD's were not set to launch them in PowerDVD and were instead launching them in Media Player where they were handled improperly.

You can manually set the AutoPlay functions in Windows 7 by going to Control Panel, then AutoPlay.  In this instance I wanted to configure the DVD movie and Enhanced DVD movie AutoPlay settings to Play DVD Video using PowerDVD 13 as shown below.

Autoplay Settings

However, it would be very time consuming to do this on 150 systems so I started off on my journey to find a way to automate the process.  I searched for hours to find a resolution to this issue.  I found very little on the web and tried to capture any registry changes with regshot but nothing showed up.  I was frustrated and pressured by other task so I gave up on the idea for a while.  About a week later after some of my critical tasks were finished I figured I would give it a go again.  One of my initial searches brought me to the following post which pointed me in the correct direction so I owe some props to jaja714 on this one.

http://www.autoitscript.com/forum/topic/145233-autoit-autoplay-program

In order to set a default application you need to add the appropriate key and string value in the following registry location

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\EventHandlersDefaultSelection

Following are the values that needed to be set for PowerDVD to AutoPlay DVD movies and Enhance DVD movies.

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\EventHandlersDefaultSelection\PlayDVDMovieOnArrival]
@="PDVD13PlayDVDMovieOnArrival"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\EventHandlersDefaultSelection\PlayEnhancedDVDOnArrival]
@="PDVD13PlayDVDMovieOnArrival"

Note that "PDVD13PlayDVDMovieOnArrival" is a handler installed by PowerDVD.  If you wish to define a custom application as the default launcher you will need to ensure that you have the proper keys also configured in the Handlers location below.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\Handlers\PDVD13PlayDVDMovieOnArrival

So we know at this point which key handles the action of setting the default player; however, there is a catch.  This key is in the Current User profile.  This means that we have a little more work ahead of us and deployment of the change depends on your environment.  Do you have static user profiles or are they volitile (deleted on logoff or on a schedule).  If you have a static user profile environmet you will want to setup a logon script to run when the user logs in to check to see if the key exists and if not apply the appropriate key.  Since the environment I am deploying to is a lab environment with volatile profiles I need to make the modification to the Default profile.

To do this you will need to load the Default user ntuser.dat (hive), import the registry key, then unload the ntuser.dat (hive).  One important thing to note here is that the Default ntuser.dat hive may get locked if a previous user has logged in so it is best to deploy this script on a freshly rebooted system.  Following is the batch script that I used to accomplish this task.

reg load HKU\hive "C:\Users\Default\ntuser.dat"
reg add HKU\hive\Software\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\EventHandlersDefaultSelection /v PlayDVDMovieOnArrival /t REG_SZ  /d "PDVD13PlayDVDMovieOnArrival" /f
reg add HKU\hive\Software\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\EventHandlersDefaultSelection /v PlayEnhancedDVDOnArrival /t REG_SZ  /d "PDVD13PlayDVDMovieOnArrival" /f
reg unload HKU\hive

In this instance when we load the hive we specified the profile name as hive.  The profile name can be anything, but it needs to be unique from any other profiles that are loaded under HKU and you will need to ensure you make the appropriate changes to your script paths if you change to another profile name.

As with most things in IT there are multiple ways to accomplish a task so I welcome any comments and suggestions regarding AutoPlay setting automation.