Description: A colleague of mine needed a way to find out what is set as the default printer for many NT4 workstations, so that he could modify static settings for some of the 3rd party software packages that we distribute. Most people are connected to network printers, although there are a few who have their own local printer.

I had to do a lot of searching (web searches) and, through trial and error, I finally came up with the following solution. It's not perfect but seems, in our environment, to be reliable with testing on several machines in different locations.

I know that Win32::Registry is considered to be obsolete, but it does the job well for this purpose.

use Win32::Registry;
use strict;

my $pval;

$::HKEY_CURRENT_USER->Open("SOFTWARE\\Microsoft\\Windows NT"
                               ."\\CurrentVersion\\Windows", $pval)
        or die "Can't open this key: $^E";

my ($type, $value);
$pval->QueryValueEx("Device", $type, $value) or die "No Device Found: 
print "Here's the default printer for this user: $value\n";
Replies are listed 'Best First'.
Re: Get Default Printer on NT4 (boo)
by boo_radley (Parson) on Aug 27, 2001 at 22:09 UTC
    very nice! here's some other possibilities for registry keys (tested under WinME)
    HKEY_CURRENT_CONFIG\System\CurrentControlSet\Control\Print\Printers\PR +INTERNAME HKEY_LOCAL_MACHINE\Config\0001\System\CurrentControlSet\Control\Print\ +Printers
Re: Get Default Printer on NT4
by jryan (Vicar) on Aug 28, 2001 at 19:00 UTC
    Do you know if this will work with NT5 (Win 2000)? This script would be the greatest if it did! :)
      It probably won't on WIN2000 because the registries are different but I will take a look tomorrow to see which is correct (I will have access to a couple of these then).
      I will let you know what I come up with.

        I am simply a newbie when it comes to perl. But I am running Windows 2000 Pro. and the script worked for me. Here is my output: Here's the default printer for this user: hp deskjet 842c series,winspool,LPT1:
      It worked on Window 2000 SP2.