0

I am writing a login script in Powershell which install a amount of printers, depending on the user currently loggin into Windows 7. I would like this user to remove the printers it have installed as soon as they are not required anymore, so as soon as this user log out.

How can I plan a script which will be executed as soon as the current sessions ends in Powershell?

Thank for your time.

PS: I cannot use AD for installing printers.

4

You can either set up a scheduled task that is triggered by Event 4634 ("logon session destroyed") in the Security Event Log, or you can assign a logoff script through the Local Group Policy Editor:

enter image description here

Logoff script details are saved in a key in the users registry hive:

HKEY_USERS\S-1-5-21-<SIDofUserInQuestion>\Software\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Logoff\<ExecutionOrder>\0

You would have to update this user-specific key for each user if you want to automate the assignment of these logoff scripts to

1

When you say you can't use AD, do you mean that you don't want to manage printers through Group Policy Preferences, or that you can't use Group Policy at all for this task? I ask because the easiest and probably most maintainable way to run a script when users log off is to add the script as a Logoff Script using Group Policy.

This TechNet article describes how to set logon and logoff scripts using Group Policy. There will be a few differences since you're adding a PowerShell script. The process will look something like this:

  1. Open Group Policy Editor.
    • If the computers that you want to set the logoff scripts on aren't joined to a domain, just run gpedit.msc from the Run dialog or a Command Prompt.
    • If you do have a domain, open Group Policy Management (gpmc.msc) either on one of your domain servers, or better yet, using Microsoft's Remote Server Administration Tools on a domain-joined workstation. Then you'll navigate to the organizational unit that you want to set the policy in, and create a new policy or edit an existing one.
  2. Once you're in Group Policy Editor, navigate to User Configuration\Windows Settings\Scripts (Logon/Logoff).
  3. Select the PowerShell Scripts tab.
  4. Add your script that removes printers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.