Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Friday, September 24, 2010

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.

2 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!!