Create a batch file to backup and restore network printers for all users











up vote
2
down vote

favorite
1












I have an XP machine with multiple profiles. These profiles sometimes have different printers mapped to each user. Example:




  • User A has network printer 1, 2 and 5 mapped. Printer 2 is default.

  • User B has network printer 1, 4 and 8 mapped. Printer 4 is default.

  • User C has network printer 2 mapped. Printer 2 is default.


My ideal would be to poll each and every user on the machine, list all the printers they have mapped, and then strip out the duplicates so that I have a list of unique printers mapped to the machine. Ideally I'd like to see which was defaulted most often, but that's totally optional. The list then would look like:



Machine.Old

- Printer 1
- Printer 2 [Defaulted most often]
- Printer 4
- Printer 5
- Printer 8


I would then like to restore the entire list of printers to each user on their new machine. They are migrating to a new windows 7 machine.



I've figured out how to do this for single user machines, with the following code:



Echo exporting printers
reg export HKCUPrintersConnections %~d0%username%printers.reg
net use >%~d0%username%mappings.txt

Echo Importing Printers
reg import %~d0%username%printers.reg


The problem is that this works for only one user. I have an admin account, and I would like to get this to work all at once for a multiprofile machine.










share|improve this question
























  • Can't you just repeat what you have for a single user for all the user subfolders in C:Documents and Settings? You might want to leave certain harcoded ones out, though, such as Guest.
    – martineau
    Dec 11 '12 at 17:54










  • In the restore I can do that, although it would be nice to get all of them mapped at once. It's mainly the backup that's the problem. I need all the users to have all the printers, and sometimes a user that rarely logged onto this machine might have no printers mapped when they will need some.
    – Wyko
    Dec 11 '12 at 18:13















up vote
2
down vote

favorite
1












I have an XP machine with multiple profiles. These profiles sometimes have different printers mapped to each user. Example:




  • User A has network printer 1, 2 and 5 mapped. Printer 2 is default.

  • User B has network printer 1, 4 and 8 mapped. Printer 4 is default.

  • User C has network printer 2 mapped. Printer 2 is default.


My ideal would be to poll each and every user on the machine, list all the printers they have mapped, and then strip out the duplicates so that I have a list of unique printers mapped to the machine. Ideally I'd like to see which was defaulted most often, but that's totally optional. The list then would look like:



Machine.Old

- Printer 1
- Printer 2 [Defaulted most often]
- Printer 4
- Printer 5
- Printer 8


I would then like to restore the entire list of printers to each user on their new machine. They are migrating to a new windows 7 machine.



I've figured out how to do this for single user machines, with the following code:



Echo exporting printers
reg export HKCUPrintersConnections %~d0%username%printers.reg
net use >%~d0%username%mappings.txt

Echo Importing Printers
reg import %~d0%username%printers.reg


The problem is that this works for only one user. I have an admin account, and I would like to get this to work all at once for a multiprofile machine.










share|improve this question
























  • Can't you just repeat what you have for a single user for all the user subfolders in C:Documents and Settings? You might want to leave certain harcoded ones out, though, such as Guest.
    – martineau
    Dec 11 '12 at 17:54










  • In the restore I can do that, although it would be nice to get all of them mapped at once. It's mainly the backup that's the problem. I need all the users to have all the printers, and sometimes a user that rarely logged onto this machine might have no printers mapped when they will need some.
    – Wyko
    Dec 11 '12 at 18:13













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I have an XP machine with multiple profiles. These profiles sometimes have different printers mapped to each user. Example:




  • User A has network printer 1, 2 and 5 mapped. Printer 2 is default.

  • User B has network printer 1, 4 and 8 mapped. Printer 4 is default.

  • User C has network printer 2 mapped. Printer 2 is default.


My ideal would be to poll each and every user on the machine, list all the printers they have mapped, and then strip out the duplicates so that I have a list of unique printers mapped to the machine. Ideally I'd like to see which was defaulted most often, but that's totally optional. The list then would look like:



Machine.Old

- Printer 1
- Printer 2 [Defaulted most often]
- Printer 4
- Printer 5
- Printer 8


I would then like to restore the entire list of printers to each user on their new machine. They are migrating to a new windows 7 machine.



I've figured out how to do this for single user machines, with the following code:



Echo exporting printers
reg export HKCUPrintersConnections %~d0%username%printers.reg
net use >%~d0%username%mappings.txt

Echo Importing Printers
reg import %~d0%username%printers.reg


The problem is that this works for only one user. I have an admin account, and I would like to get this to work all at once for a multiprofile machine.










share|improve this question















I have an XP machine with multiple profiles. These profiles sometimes have different printers mapped to each user. Example:




  • User A has network printer 1, 2 and 5 mapped. Printer 2 is default.

  • User B has network printer 1, 4 and 8 mapped. Printer 4 is default.

  • User C has network printer 2 mapped. Printer 2 is default.


My ideal would be to poll each and every user on the machine, list all the printers they have mapped, and then strip out the duplicates so that I have a list of unique printers mapped to the machine. Ideally I'd like to see which was defaulted most often, but that's totally optional. The list then would look like:



Machine.Old

- Printer 1
- Printer 2 [Defaulted most often]
- Printer 4
- Printer 5
- Printer 8


I would then like to restore the entire list of printers to each user on their new machine. They are migrating to a new windows 7 machine.



I've figured out how to do this for single user machines, with the following code:



Echo exporting printers
reg export HKCUPrintersConnections %~d0%username%printers.reg
net use >%~d0%username%mappings.txt

Echo Importing Printers
reg import %~d0%username%printers.reg


The problem is that this works for only one user. I have an admin account, and I would like to get this to work all at once for a multiprofile machine.







networking batch-file printer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 22 at 12:07









Hennes

58.8k792141




58.8k792141










asked Dec 11 '12 at 17:18









Wyko

1114




1114












  • Can't you just repeat what you have for a single user for all the user subfolders in C:Documents and Settings? You might want to leave certain harcoded ones out, though, such as Guest.
    – martineau
    Dec 11 '12 at 17:54










  • In the restore I can do that, although it would be nice to get all of them mapped at once. It's mainly the backup that's the problem. I need all the users to have all the printers, and sometimes a user that rarely logged onto this machine might have no printers mapped when they will need some.
    – Wyko
    Dec 11 '12 at 18:13


















  • Can't you just repeat what you have for a single user for all the user subfolders in C:Documents and Settings? You might want to leave certain harcoded ones out, though, such as Guest.
    – martineau
    Dec 11 '12 at 17:54










  • In the restore I can do that, although it would be nice to get all of them mapped at once. It's mainly the backup that's the problem. I need all the users to have all the printers, and sometimes a user that rarely logged onto this machine might have no printers mapped when they will need some.
    – Wyko
    Dec 11 '12 at 18:13
















Can't you just repeat what you have for a single user for all the user subfolders in C:Documents and Settings? You might want to leave certain harcoded ones out, though, such as Guest.
– martineau
Dec 11 '12 at 17:54




Can't you just repeat what you have for a single user for all the user subfolders in C:Documents and Settings? You might want to leave certain harcoded ones out, though, such as Guest.
– martineau
Dec 11 '12 at 17:54












In the restore I can do that, although it would be nice to get all of them mapped at once. It's mainly the backup that's the problem. I need all the users to have all the printers, and sometimes a user that rarely logged onto this machine might have no printers mapped when they will need some.
– Wyko
Dec 11 '12 at 18:13




In the restore I can do that, although it would be nice to get all of them mapped at once. It's mainly the backup that's the problem. I need all the users to have all the printers, and sometimes a user that rarely logged onto this machine might have no printers mapped when they will need some.
– Wyko
Dec 11 '12 at 18:13










1 Answer
1






active

oldest

votes

















up vote
0
down vote













To access the registry keys of the other users on the system you will have to load the registry hive per each user. See reg /? The user's hive file NTUser.dat will be located at the base of each user's directory. C:Documents and Settings<User>NTUser.dat. Note that it is a hidden system file.



I do not have the time to create an entire report, but here is how to export the printers for all of the users. This will loop through every user directory, load their registry hive, and export the printers to a user directory at the base of the drive.



@echo off
pushd "C:Documments and Settings"
for /d %%A in (*) do call :ExportPrinters "%%~dpnA" "%~d0%%~nA"
popd
goto End


:ExportPrinters <UserDir> <Target>
setlocal
set "xUserDir=%~1"
set "xTarget=%~2"
if not defined xUserDir goto :eof
if not exist "%xUserDir%" goto :eof
if not defined xTarget goto :eof
if not exist "%xTarget%" goto :eof
reg load "HKUTempHive" "%xUserDir%NTUser.dat"
reg export "HKUTempHivePrintersConnections" "%xTarget%printers.reg"
reg unload "HKUTempHive"
endlocal
goto :eof


:End
pause





share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f517933%2fcreate-a-batch-file-to-backup-and-restore-network-printers-for-all-users%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    To access the registry keys of the other users on the system you will have to load the registry hive per each user. See reg /? The user's hive file NTUser.dat will be located at the base of each user's directory. C:Documents and Settings<User>NTUser.dat. Note that it is a hidden system file.



    I do not have the time to create an entire report, but here is how to export the printers for all of the users. This will loop through every user directory, load their registry hive, and export the printers to a user directory at the base of the drive.



    @echo off
    pushd "C:Documments and Settings"
    for /d %%A in (*) do call :ExportPrinters "%%~dpnA" "%~d0%%~nA"
    popd
    goto End


    :ExportPrinters <UserDir> <Target>
    setlocal
    set "xUserDir=%~1"
    set "xTarget=%~2"
    if not defined xUserDir goto :eof
    if not exist "%xUserDir%" goto :eof
    if not defined xTarget goto :eof
    if not exist "%xTarget%" goto :eof
    reg load "HKUTempHive" "%xUserDir%NTUser.dat"
    reg export "HKUTempHivePrintersConnections" "%xTarget%printers.reg"
    reg unload "HKUTempHive"
    endlocal
    goto :eof


    :End
    pause





    share|improve this answer

























      up vote
      0
      down vote













      To access the registry keys of the other users on the system you will have to load the registry hive per each user. See reg /? The user's hive file NTUser.dat will be located at the base of each user's directory. C:Documents and Settings<User>NTUser.dat. Note that it is a hidden system file.



      I do not have the time to create an entire report, but here is how to export the printers for all of the users. This will loop through every user directory, load their registry hive, and export the printers to a user directory at the base of the drive.



      @echo off
      pushd "C:Documments and Settings"
      for /d %%A in (*) do call :ExportPrinters "%%~dpnA" "%~d0%%~nA"
      popd
      goto End


      :ExportPrinters <UserDir> <Target>
      setlocal
      set "xUserDir=%~1"
      set "xTarget=%~2"
      if not defined xUserDir goto :eof
      if not exist "%xUserDir%" goto :eof
      if not defined xTarget goto :eof
      if not exist "%xTarget%" goto :eof
      reg load "HKUTempHive" "%xUserDir%NTUser.dat"
      reg export "HKUTempHivePrintersConnections" "%xTarget%printers.reg"
      reg unload "HKUTempHive"
      endlocal
      goto :eof


      :End
      pause





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        To access the registry keys of the other users on the system you will have to load the registry hive per each user. See reg /? The user's hive file NTUser.dat will be located at the base of each user's directory. C:Documents and Settings<User>NTUser.dat. Note that it is a hidden system file.



        I do not have the time to create an entire report, but here is how to export the printers for all of the users. This will loop through every user directory, load their registry hive, and export the printers to a user directory at the base of the drive.



        @echo off
        pushd "C:Documments and Settings"
        for /d %%A in (*) do call :ExportPrinters "%%~dpnA" "%~d0%%~nA"
        popd
        goto End


        :ExportPrinters <UserDir> <Target>
        setlocal
        set "xUserDir=%~1"
        set "xTarget=%~2"
        if not defined xUserDir goto :eof
        if not exist "%xUserDir%" goto :eof
        if not defined xTarget goto :eof
        if not exist "%xTarget%" goto :eof
        reg load "HKUTempHive" "%xUserDir%NTUser.dat"
        reg export "HKUTempHivePrintersConnections" "%xTarget%printers.reg"
        reg unload "HKUTempHive"
        endlocal
        goto :eof


        :End
        pause





        share|improve this answer












        To access the registry keys of the other users on the system you will have to load the registry hive per each user. See reg /? The user's hive file NTUser.dat will be located at the base of each user's directory. C:Documents and Settings<User>NTUser.dat. Note that it is a hidden system file.



        I do not have the time to create an entire report, but here is how to export the printers for all of the users. This will loop through every user directory, load their registry hive, and export the printers to a user directory at the base of the drive.



        @echo off
        pushd "C:Documments and Settings"
        for /d %%A in (*) do call :ExportPrinters "%%~dpnA" "%~d0%%~nA"
        popd
        goto End


        :ExportPrinters <UserDir> <Target>
        setlocal
        set "xUserDir=%~1"
        set "xTarget=%~2"
        if not defined xUserDir goto :eof
        if not exist "%xUserDir%" goto :eof
        if not defined xTarget goto :eof
        if not exist "%xTarget%" goto :eof
        reg load "HKUTempHive" "%xUserDir%NTUser.dat"
        reg export "HKUTempHivePrintersConnections" "%xTarget%printers.reg"
        reg unload "HKUTempHive"
        endlocal
        goto :eof


        :End
        pause






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 13 '12 at 1:30









        David Ruhmann

        1,099715




        1,099715






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f517933%2fcreate-a-batch-file-to-backup-and-restore-network-printers-for-all-users%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?