Skip to main content

Set default printers with Powershell

The original question from class is how to set the default printer for your users. I’ve been working through different types of PowerShell remoting with no success. The actual method in the WMI class Win32_Printer will not let you set the default printer from a remote client. I attempted to use both remote sessions and the invoke-command cmdlet.

I finally decided to do it via group policy. The code for the script dropped significantly when I went to this approach.

There are a few requirements to this process. You need to have a Windows Server 2008 R2 and this can only be applied to your Windows 7 clients. Below is the script:

<#

=============================================

Script Name: DefaultPrinter.ps1

Auther: Jason A. Yoder, MCT

MCTExpert, Inc.

Website: www.MCTExpert.com

Blog: www.MCTExpert.blogspot.com

Date: 2010AUG22

Script Purpose:

Set the default printer on clients.

Requirments:

This script is designed to be used as a logon

script in group policy.

You must have a Windows Server 2008 R2 DC.

This will only work on domain joined Window 7

Clients.

#>

# Global Variables ==========================

# Enter the name of the printer to be set as

# the default printer here.

$TargetPrinter = "Microsoft XPS Document Writer"

# Main Code =================================

# Set Error handling to allow for a client without the

# target printer installed.

$ErrorActionPreference = “SilentlyContinue”

$LocalPrinter = GWMI -class Win32_Printer |

Where {$_.Name -eq $TargetPrinter}

$LocalPrinter.SetDefaultPrinter()

$ErrorActionPreference = “Stop”

# Return Error handling to stop if error detected.

# End of Script ===============================

By setting the $TargetPrinter to the display name of the printer you want to set as the default, you can apply this to your Windows 7 clients. The $ErrorActionPreference will prevent clients that do not have the targeted printer installed from displaying an error message to the user.

Here is how to do it.

Copy this code over to notepad. The colors will disappear. The colors are used in the PowerShell ISE and will not change the performance of the script. In order for this script to be available to all clients and users, you need to save it in a special location. The location is in the SYSVOL folder of one of your Domain Controllers. You can find this location on a Domain Controller at C:\Windows\SYSVOL\SYSVOL\YourDomainName\Scripts. Make sure the extension on this script is .ps1.

Next you need to create a Group Policy on your Domain controller.

Click Start \ Administrative Tools \ Group Policy Managent

Expand Domains \ YourDomainName \ Group Policy Objects.

Right click Group Policy Objects and click New.

Give the GPO a name and click OK.

Right click the new GPO you just created and click Edit.

You need to decide if you want to apply this by computer, or user. For the rest of this instruction, we are going to apply this to a user.

Expand User Configuration \ Policies \ Windows Settings \ Scripts (Logon/Logoff)

Double click Logon.

Click the PowerShell Scripts tab.

Click Add.

Click Browse and surf to the location of the script that you saved in SYSVOL.

Click OK.

Click OK.

Close the Group Policy Management Editor window.

You now need to link this GPO to the OU that contains the user accounts that you want it to apply to. Remember, you cannot link a GPO to the default USERS container in Active Directory. You must move your user accounts to an OU and apply this policy to that OU. You can use Security Filtering to only apply the policy to a select group of users in an OU without affecting all of them.

Comments

Sean said…
Thanks so much! This script worked for me and should save me a lot of trouble at work!
Mark B said…
Hey! Just wanted to say thanks. I recently moved my printers to computer configuration side deployment to speed up system logon times in our clinic. This created a default printer problem for me. With the help of this script I've been able to apply default selections while preserving my fast logon times. Thank you for sharing!!

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.