SetDefaultPrinter using Pinvoke and Powershell

[code language=”powershell” wraplines=”false” collapse=”false”]
$code = @’
using System;
using System.Runtime.InteropServices;
namespace ChangeDefaultPrinter
public class Program
[DllImport("Winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
private static extern bool SetDefaultPrinter(string printerName);
public static bool Run(string PrinterName)
bool default_result = SetDefaultPrinter(PrinterName);
return default_result;
Add-Type -TypeDefinition $code -Language CSharp
#Set Default Printer – for network printers use \\PrintServer\PrintQueue
if(!([ChangeDefaultPrinter.Program]::Run("Canon iR-ADV C5045/5051 PCL5c")))
throw (New-Object componentmodel.win32Exception )

Leave a comment

Your email address will not be published. Required fields are marked *

WordPress Appliance - Powered by TurnKey Linux