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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...